Forum Moderators: coopster

Message Too Old, No Replies

Global Variable Variables

How do I do Global Variable Variables.

         

adammills

8:19 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



I want to do something simular to the below. I am having issue with the final line of code that should echo out the value of $your_name_error, which is my variable variable set in setError(). Please help.


$a = "";
$your_name = "";
function hasContent($x, $y){
if(strlen($x) == 0){
global $a;
$a = $y."_error";
setError($y, "This Field has to have content.");
}
}
function setError($x, $y){
global $$a;
$$a .= $y;
}

hasContent($your_name,"your_name");
echo $your_name_error;

adammills

2:37 am on Jan 23, 2008 (gmt 0)

10+ Year Member



still having a hard time with this perhaps I'm doing it wrong, maybe I need to simplify the above to get an answer as well:

<?
$a = "jim";

function myFunction(){
global $$a;
$$a = "my string";
}

echo $jim;
?>

Still doesn't work ... seems like it should ... :/ ... if someone could help please do. Problme is still that $jim have no value outside of the function ... need ot make it global, don't know how ... but is isn't always going to be $jim ... hence the variable variable problem ...

cameraman

9:28 am on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, adammills!

Using your second example:
function myFunction(){
global $$a;
$$a = "my string";
}

$a is undefined in your function. Try this:
function myFunction(){
global $a;
global $$a;
$$a = "my string";
}

adammills

3:14 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



thanks for the replay will try it out shortly

worked great, thank you. Somtimes it is the little things you miss.

adammills

9:55 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



Ok ... the problem I thought I had, wasn't my problem. I am having issues changing values of variables passed to a function, then echoing them out.


$your_name = "";
$your_name_error = "Nothing";
function hasContent($x, $y){
if(strlen($x) == 0){
$y = "This Field has to have content.";
}
}

hasContent($your_name,$your_name_error);

echo = $your_name_error;

I'd want that to echo out "This Field has to have content." ... but is continues to echo out "Nothing" even though it does get into the check the sets it. What am I doing wrong here?

PHP_Chimp

10:07 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$your_name = "";
$your_name_error = "Nothing";
function hasContent($x, $y){
if(strlen($x) == 0){
$y = "This Field has to have content.";
}
}
hasContent($your_name,$your_name_error);
echo = $your_name_error;

As you are echoing $your_name_error you are getting the value that you have set on the second line of your code.

You could try echo $y;

Could you not use something like -


echo $your_name? $your_name : 'This field has to have content';

As if $your_name is false (or any of the other things that evaluate as false) then you will get the 'This field ...' message.

[edited by: PHP_Chimp at 10:10 pm (utc) on Jan. 23, 2008]

pixeline

10:18 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



Hi Adam,

i think you're confused about how to do what you want to do: serverside validation. And you 're doing it the complicated way.

What you want is check each submitted field, validate those that needs to be validated, and if there are errors, cancel the SAVE and display the errors.

Here is a sample on how to do that: depending on the form's method (POST or GET), the submitted values are sent in an array (in php: $_POST or $_GET). What we'll do is store in another associated array each error, with the same key as your forms variables.

To get back to your example, say user should submit an email, but forgets to write the message and hit "submit". The form is sent to the server, the server detects there is no message so it shows the form back with the error message.


var $errors= array();
$message= trim($_GET['email_message']);
$subject= trim($_GET['email_subject']);
if (strlen($message)<1){
// no message! send an error, make him bite its fingers!
$errors['email_message']='You must write a message, fool!';
}
if (strlen($subject)<1){
// no message! send an error, make him bite its fingers!
$errors['email_subject']='You must put a subject to your emai , idiot!';
}

// other validations....
...


Now check if the $errors array is empty. If it is, then send the mail, otherwise, show the form back and display errors

if(count($errors)<1){
mail($to,$from,$subject,$message);
die("message sent!");
}else
{
echo '<pre>';
print_r($errors);
echo '</pre>';
}

// now display your html form

in your html form you could also do that:


<textarea name="email_message"></textarea>
<? if (isset($errors['email_message'])){
echo '<p style="color:red;font-size:200%">'.$errors['email_message'].'</p>';

This is very raw and must be refined but you get the idea, no?

adammills

10:23 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



pixel, you posted while I was typing, reading yours now.

[edited content out]

That makes sense pixeline! Thanks!

cameraman

10:40 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And to answer your last question:
function foo($x) {
$x = 7;
}

$a = 1;
foo($a);
echo $a; // echos 1

because the function modifies a local copy of the variable that's passed.

To modify the variable passed, pass it by reference:

function foo(&$x) {
$x = 7;
}

$a = 1;
foo($a);
echo $a; // echos 7

phnord

6:05 am on Jan 25, 2008 (gmt 0)

10+ Year Member



I believe the "global" modifier has been deprecated with newer versions of PHP and is currently being phased out. Use $GLOBALS instead..or define a constant via define().