Page 1 of 1

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

Posted: Tue Nov 15, 2005 4:37 pm
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.

Posted: Tue Nov 15, 2005 4:50 pm
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']);

Posted: Tue Nov 15, 2005 6:00 pm
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

Posted: Wed Nov 16, 2005 4:46 am
by Jenk
When you call the function, are you passing the array as an argument?