php - edit $name values to $name2 - php

Already asked but question was closed so here it is again:
I need to understand the code that renames values.
For example, I have $name which outputs 'A Long Way From Home', in case the script encounter this value I need to rename the value to 'LongWay34' into $name2 so I can use it somewhere else.
There's no need for any uber-script, I have just a few elements I need to modify and I can write a line for each one!
I hope I've been clear, thanks in advance for the help!
k

if($name === 'A Long Way From Home') $name2 = 'LongWay34';
...?

There is no such thing like rename in PHP.
There are variables, and you can assign a value to them.
Variables can hold data. To put data into variable you assign something to it. Examples:
$variable1 = "text value";
$variable2 = 24; //number value
$variable3 = $othervariable; // assign value of one variable to another (like "copy")
You can't rename variable. You can create new variable with diffrent name and assign old variable value to the new one.
$new_variable = $old_variable;
If I understand you correctly - you want code, that will run when variable value will become "some value". Normal variable don't have any "trigger" features, that will trigger some code. You have to check value of that variable and make conditional blocks (if for example).
There are special "variable" types, that can monitor changes of variable, run some additional code when variables are accessed. They are called classes, but now this may be too complicated for you to understand.

Related

Is there a way to access a PHP variable's name and use it to get a POST variable of the same name?

I know this can't be best practice, but out of curiosity, is there a way to loop through several PHP variables and assign them to a POST variable of the same name?
For example, if I were to need to do something like
$var1 = $_POST["var1"]
I might be able to loop through all of my variables and do
foreach($values_array as &$value) {
if isset($_POST[valueName($value)]) {
$value = $_POST[valueName($value)];
} else {
$value = "";
}
}
In the end, I'm just not sure what the best way to check and assign a large number of POST values (I'm looking at over 50 right now...). Pointers to correct way would be greatly appreciated as well.
You can use extract() for a much simpler and safer approach:
extract($_POST, EXTR_SKIP); // "Safest" way
You can use the EXTR_IF_EXISTS flag to only overwrite existing variables, or you could use EXTR_SKIP to prevent writing into any variables that already exist (for best safety.) You can also prefix all of the variables:
extract($_POST, EXTR_PREFIX_ALL, 'post');
Then a $_POST['variable'] would be available through $postvariable - but be wary - for example, you must ensure that none of your PHP variables start with "post"
The normal practice is to leave the $_POST variables where they are and access them directly until you begin manipulating, validating, and sanitizing them.

Best method to recognize unused variables

I have code where I use some variables. Example:
$name = "someName";
$output = sprintf($doingText, $name); // $doingText is here undefined
I want to search the code for surely undefined variables (some sort of static code analysing).
These variables should be all some language text. No problem until there, but I don't want to make manually a list which variables exist: I want to get the variable names and then make some html form where I can see them and put into database in variablename-text pairs.
Question is: how to search them? (I haven't found any script which is able to do this in PHP by googling...)
(p.s.: I don't know what is the best method to search them as there may not be only assignments by =, but also with foreach ($arr as $val) etc.)
Why not use an IDE like NetBeans? That will actively check if you have unused variables. So while your coding it will show you in real time, rather then finish the script and find out you have x amount of errors/unused variables. Just food for thought.

Is it possible to print php variable withing a variable?

I have a very simple question. But is really making me crazy.
I have a statement say:
example and example with one php variable like $loggedin_user_name
First of all, I want to store the above sentence in MySQL database and then take it back whenever I want to print the above statement. It seems that their is no issue.
But when I tried to print data after extracting from database it is printing the same statement. But i guess, it has to print the logged in user name instead of $loggedin_user_name in the above statement.
So, is it possible to print the variable within the variable? If yes, please suggest a way.
use sprintf()
$str = "example and example with one php variable like %s";
Then load it from database and fill
$out = sprintf($str, $loggedin_user_name);
If it is always the same variable name, I would suggest using
echo str_replace($fromDb, '$variableToReplace', $variableToReplace);
You can use preg_match to find you variable name in string and then replace it with str_replace.
$name = "ABC";
$bla = "$name";
echo $bla; //ABC
Will always be "ABC", because PHP is evaluating your variable when asigning to $bla.
You can use single-quotes to avoid that behaviour (like $bla='$name'; //$name) or you quote the $-sign (like $bla="\$name"; //$name). Then you can store your string like you wanted into your database.
But you can not (only when using eval(), wich you MUST NOT DO in good PHP-Code) build this behaviour, that php has, when printing fulltext.
Like Mentioned in another answer, you should use printf or sprintf and replace the $loggedin_user_name with %s (for "string).
Best would be to concatinate a string:
$exampleWithUsername = 'example' . $loggedin_user_name;
echo $exampleWithUsername;
'example' is a hardcoded string, but you can give it a variable containing string $example, or directly concatinate $username into $example.
You can use eval function, it can be used like your example:
$loggedin_user_name = 'bilal';
$str = "example and example with one php variable like $loggedin_user_name";
eval("\$str = \"$str\";");
echo $str;
Cons:
If your str variable or string/code which you give to eval as a parameter is filled by users, this usage creates a vulnerability.
In case of a fatal error in the evaluated code, the whole script exits.

Variables in PHP Array?

So i have the code below basically when its run it will display a graph. How can i make the variables inside the arrays work the variable works and when echoed will give a number but for some reason it doesn't input number there. $mar1 in [here]
$lineChart = new gLineChart($_GET['width'],$_GET['height']);
[here]$lineChart->addDataSet(array($mar1,315,66,40));[/here]
$lineChart->setLegend(array("first"));
$lineChart->setColors(array("ff3344", "11ff11", "22aacc", "3333aa"));
$lineChart->setVisibleAxes(array('x','y'));
$lineChart->setDataRange(30,400);
$lineChart->addAxisLabel(0, array("This", "axis", "has", "labels!"));
$lineChart->addAxisRange(1, 30, 400);
$lineChart->setGridLines(0, 15);
$lineChart->renderImage();
This is a very, very basic question about PHP syntax. Arrays can and frequently are used with variable data.
There isn't anything wrong with the syntax of the code that you posted, so chances are that this is a case of the $mar1 variable not being defined or not containing the data that you're expecting. You probably want to echo or var_dump this variable and see what's in it and work backwards from there.
If $mar1 doesn't contain what you expect, look in the code above that line and see if its value is being set. If this is being passed in the browser's query string like the $_GET['width'] and $_GET['height'] variables are, you would need to access it as $_GET['mar1'] instead of just $mar1.
If this file is being included from another file or includes/requires other files, it could also be defined in the including file(s).
If $mar1 does contain the value you're expecting, then check the documentation for the gLineChart class and make sure that you're passing it all the correct parameters.

PHP Variable used in a variable

I'm new to PHP so please forgive me if this is a stupid question.
How do you include a variable in a variable?
What I mean is:
<?php
$variable_a = 'Adam';
$variable_b = '$variable_a';
?>
In other words the second variable is the same as the first one.
I won't bother explaining why I need to do it (it will confuse you!), but I just want to know firstly if it's possible, and secondly how to do it, because I know that code there doesn't work.
Cheers,
Adam.
Don't use the quotes, they indicate a string. Just point to the variable directly, like this:
$variable_b = $variable_a;
If you want the variables to be equal, use:
$variable_b = $variable_a;
If you want the second variable to contain the first, use variable parsing:
$variable_b = "my other variable is: $variable_a";
Or concatenation:
$variable_b = 'my other variable is: ' . $variable_a;
PHP have this advantage in producing one string variable's value based on another. To do this, write code like this:
$b = "My name is $name.";
The following code does NOT work:
$b = '$name';
Other occasions in which coding like this works are:
$b = <<<STRING
Hello, my name is $name...
STRING;
If you want to access an array, use:
$b = "My ID is {$id['John Smith']}.";
and of course,
$b = <<<STRING
Hello, my name is {$username}, my ID is {$id['John Smith']}.
STRING;
I recommend using {} because I frequently use Chinese charset in which occasion coding like
$b = "我是$age了。";
will cause PHP look up for variable $age了。 and cause error.
Either without quotes to reference the variable directly, since quotations means it's a string
$variable_b = $variable_a;
Or you can ommit the variable in double quotations, if you want it to appear in a string.
$variable_b = "My name is $variable_a";

Categories