Yarin
Yarin

Reputation: 183769

Is there another way to assign PHP variables that I don't know about?

I'm debugging some client PHP code that has me stumped. As this pseudo-example illustrates, there is code on the receiving end of a form submission that processes the form values, but without ever apparently assigning those values:

(On form submission, where form has fields 'name' and 'position'):

echo "The name is = ". $name;
echo "The position is = ". $position;

This code is part of a large app, and code from other files is called before this. BUT the crucial point is that if I do a search for '$name = ' across the entire codebase it never shows up. How then is it possible that the request variable gets assigned to $name? Is there another way to assign value to a variable in PHP other than $var = value??

My only other clue is that this project uses Smarty, which I don't know anything about.

Upvotes: 1

Views: 217

Answers (8)

itsmequinn
itsmequinn

Reputation: 1084

It may be that the person that created the code was working on a server with register_globals on. What that does, for example, is create regular global variables as the result of a form submission rather than populating the $_POST or $_GET arrays.

Upvotes: 5

Eugen Rieck
Eugen Rieck

Reputation: 65304

Maybe this is another outbreak of the register_globals disease?

This has been thought to have died out, but surfaces again and again!

Upvotes: 2

Hammerite
Hammerite

Reputation: 22340

Technically yes there is.

$x = 'name';
$$x = 'harry';
echo 'Yer a wizard '.$name;

(I would be surprised if this was the reason)

Upvotes: 3

dbrumann
dbrumann

Reputation: 17166

When you look at the smarty documentation you can see that variables are assigned like this (copied from the linked page):

<?php

$smarty = new Smarty();

$smarty->assign('firstname', 'Doug');
$smarty->assign('lastname', 'Evans');
$smarty->assign('meetingPlace', 'New York');

$smarty->display('index.tpl');

?>

Upvotes: 3

Michael Robinson
Michael Robinson

Reputation: 29498

Try turning your error reporting level up, if these variables are being used but haven't been initialised, a warning will be shown

Upvotes: 1

Jacob Mattison
Jacob Mattison

Reputation: 51062

If the register_globals directive has been set to true in php.ini, then all POST attributes will also show up as individual variables, in the way you describe. Note that this directive is false by default, and that use of this directive is deprecated.

Upvotes: 5

Jasmo
Jasmo

Reputation: 828

I guess your server has register_globals setting on, which automatically generates variables from posted items:

$_POST['foo'] === $foo

Upvotes: 2

user321531
user321531

Reputation:

Sounds to me like someone included those variables with the intention of doing more with them, but never got around to doing so. Unless an include php page is assigning values to those variables, nothing will go in them.

Upvotes: 1

Related Questions