The array exists and has 13 elements but not when passed...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Coxster
Forum Newbie
Posts: 14
Joined: Tue Nov 01, 2005 3:22 am

The array exists and has 13 elements but not when passed...

Post by Coxster »

I'm passing an array to a function (or I want to). I'm trying to avoid using

Code: Select all

global ...
in the function.

Here's the function call:

Code: Select all

loginForm($currentLoginText, $self);

Code: Select all

echo "currentLoginText length: " . count($currentLoginText) . "<br>";
echos

Code: Select all

currentLoginText length: 13
but inside the function

Code: Select all

echo "textArray length: " . count($textArray) . "<br>";
echos

Code: Select all

textArray length: 0
The function so far is:

Code: Select all

function loginForm($textArray, $actionValue)
	{
		echo "textArray length: " . count($textArray) . "<br>";
		// Start displaying form... rest of function	
               }
Why has the array no elements inside the function?

Incase it matters the array elements are not numbered, they are named, ie

Code: Select all

$currentLoginText['loginMessage']
Thanks.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If things are acting weird here is a bit of advise, start your script with the following:

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
?>
If you want to know the length of a string, you should use strlen instead.

Code: Select all

function loginForm($textArea) {
  echo "the textarea length is : " . strlen($textArea)";
}

$someArray['blah'] = 'phpdn is cool';
loginForm($someArray['blah']);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's also a good idea to check that you are passing what you expect by adding:

Code: Select all

echo '<pre>';
print_r($textArray);
echo '</pre>';
to the start of the function.

Mac
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

When you call the function, are you passing the array as an argument?
Post Reply