What does this php code means - php

Hello I am new to PHP and I don't know exactaly what does this code means
$de = array('Ä'=>'ae','ä'=>'ae','Ü'=>'ue','ü'=>'ue', 'Ö'=>'oe', 'ö'=>'oe', 'ß'=>'ss');
strtr($str, ${$de});
The only thing that I need to know is what does ${$de} mean?

It's a variable variable, one of the most intriguing part of the php implementation.
Sometimes usefull, always confusing:
$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";
$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a
$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
....

That's a variable variable.
http://php.net/manual/en/language.variables.variable.php

Known as Variables Variable
http://php.net/manual/en/language.variables.variable.php

Related

How can I make a variable's name from another variable AND string?

Say I had the variable $foo which has a value of "bar" and I wanted to make a variable variable from it, but also append the string "123" to it, so that the variable name is $bar123. How would I do this?
I already know that $$foo = "abc123" would make a variable $bar with the value of "abc123", but I don't know how to append a string to this variable name.
Using variable variables, you can do something as the following:
<?php
$a = "foo";
$number = 123;
$b = $a . "$number";
$$b = "Hello World!";
echo ${$b};
However, as #smith said, it is better to use associative arrays here.
I realised the solution was fairly simple:
$foo = "bar";
$x = $foo . "123"
$$x = "random important variable value"

PHP, $this->{$var} -- what does that mean?

I have encountered the need to access/change a variable as such:
$this->{$var}
The context is with CI datamapper get rules. I can't seem to find what this syntax actually does. What do the {'s do in this context?
Why can't you just use:
$this->var
This is a variable variable, such that you will end up with $this->{value-of-$val}.
See: http://php.net/manual/en/language.variables.variable.php
So for example:
$this->a = "hello";
$this->b = "hi";
$this->val = "howdy";
$val = "a";
echo $this->{$val}; // outputs "hello"
$val = "b";
echo $this->{$val}; // outputs "hi"
echo $this->val; // outputs "howdy"
echo $this->{"val"}; // also outputs "howdy"
Working example: http://3v4l.org/QNds9
This of course is working within a class context. You can use variable variables in a local context just as easily like this:
$a = "hello";
$b = "hi";
$val = "a";
echo $$val; // outputs "hello"
$val = "b";
echo $$val; // outputs "hi"
Working example: http://3v4l.org/n16sk
First of all $this->{$var} and $this->var are two very different things. The latter will request the var class variable while the other will request the name of the variable contained in the string of $var. If $var is the string 'foo' then it will request $this->foo and so on.
This is useful for dynamic programming (when you know the name of the variable only at runtime). But the classic {} notation in a string context is very powerful especially when you have weird variable names:
${'y - x'} = 'Ok';
$var = 'y - x';
echo ${$var};
will print Ok even if the variable name y - x isn't valid because of the spaces and the - character.

check if string is variable or just a string

I have got a variable called $Title
It is possible that the variable contains a string,
example A: 'Foo'
But the variable can also contain a reference to an different variable,
example B: '$Foo'
When I use print $Title php returns 'Foo' (EX A) or '$Foo' (EX B) as an string.
When I use print $$Title php tries to return the value of a variable named $Foo (EX A) or $$Foo (EX B)
I want to accomplish the following:
When $Title contains just a string, print that string
When $Title contains the reference to a variable, look up that variable and show its content
I could just look for the first character in the string. When it is $ use echo $$Title ELSE use echo $Title, but it is possible that $Title contains something like this:
$Title = '$Foo . \'Bar\' . $Bar . \'Foo\'';
In that case $Foo and $Bar are variables and need to act as such, 'Bar' and 'Foo' are strings and need to act as such.
How can I make this able to work??
A string is always just a string. A string is never a variable.
Case 1, a plain string:
$foo = 'bar';
echo $foo; // bar
echo $$foo; // content of $bar if it exists
Case 2, a "variable in a string":
$foo = 'bar';
$bar = "$foo"; // $bar is now the string 'bar', the variable is interpolated immediately
echo $bar; // bar
echo $$bar; // bar (content of $bar)
Case 3, a string with a dollar in it:
$foo = '$bar';
echo $foo; // $bar
echo $$foo; // invalid variable name "$bar"
$$foo resolves to the variable name $$bar, which is an invalid name.
You cannot have "variables in strings". Writing "$foo" immediately interpolates the value of $foo and gives you back a new string.
Just maybe, you want this:
$foo = 'bar'; // the string "bar"
$baz = '$foo'; // the string "$foo"
// MAGIC
echo $baz; // echoes "bar"
I.e., if your string contains a dollar followed by the name of a variable, you want to substitute that value. First I'd say this is a bad idea. Then I'd say you will have to extract all those "dollar strings" out of your string, check if the variable exists, then replace the value in the string using normal string manipulation. Yes, you could do it using eval, but no, that's not a good idea. For the above code, something like this'll do:
if ($baz[0] == '$') {
$varName = substr($baz, 1);
if (isset($$varName)) {
$baz = $$varName;
}
}
The is_string PHP function is used to check if a value is a string. This could be used within an if () statement to treat strings in one way and non-strings in another. It will return true or false.
<?php
if (is_string(23))
{
echo "Yes";
} else {
echo "No";
}
?>
The code above should output "No" because 23 is not a string. Let's try this again:
<?php
if (is_string("Hello World"))
{
echo "Yes";
} else {
echo "No";
}
?>
Since "Hello World" is a string, this would echo "Yes".
Use a if statement to check if the $variable is a string..
if(is_string($var)) {
echo $var;
} else {
// What do you want to achieve here?
}
Code like this:
$Title = '$Foo . \'Bar\' . $Bar . \'Foo\'';
can't be evaluated when you try to print it, it's evaluated at the moment of assignment. The reason variable names are not being replaced by their values in your case are single quotes.
$a = 1;
$b = 2;
$var = '$a + $b'; // this is a string
echo $var; // $a + $b
$var = "$a + $b"; // this is also a string, but variables will be processed
echo $var; // 1 + 2
Note, that in second scenario it only processes the variable names, it doesn't run the code ('+' is a string, not an operation).
If you want to keep the '$a + $b' as a string within your $title and evaluate it as a PHP code at the moment, when you print it, you need to use eval function. However, I strongly suggest trying to avoid using this function as much as possible.
As I can understand, string may be just string or some sort of variable 'reference'.
Will this work for you or there is always $ if variable reference?
$var1='test';
$ref1='var1';
if(isset($$ref1)) {
// variable exists
}
else {
// no such variable
}
You can use the dangerous eval php contruct. But be warned if any of the string is coming from a user input
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
This is just a copy and paste from (PHP eval documentation)[http://php.net/manual/en/function.eval.php]

What is the difference between $a and $$a in php? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does $$ mean in PHP?
I am new to PHP and I don't know what the difference between $a and $$a is.
$a represents a variable
$$a represents a variable with the content of $a
example:
$test = "hello world";
$a = "test";
echo $$a;
output will be hello world
If $a = 'b' then $$a is $b.
This is a variable variable. They are evil. Use arrays instead (which do the same thing, but more maintainably and with the ability to use array functions on them).
$variable is a normal variable
$$variable takes the value of a variable and treats that as the name of a variable
eg:
$var = 'welcome';
echo $var //prints welcome
$$var = 'to stackoverflow';
echo "$var ${$var}"; //prints welcome to stackoverflow
echo "$var $welcome"; //prints welcome to stackoverflow
Double dollar is a powerful way to programmatically create variables and assign values them.
E.g:
<?php
$a = “amount”;
$$a =1000;
echo $amount; //echo’s 1000 on screen
?>
In the example above, you can see that the variable $a stores the value “amount”. The moment you use a double dollar sign ($$) you are indirectly referencing to the value of $a i.e. amount.
So, with this like $$a = 1000; the variable $amount gets created and I assign the value 1000 to $amount. This way you can programmatically create variables and assign values to them.
$a is the contents of the variable a, $$a is the contents of the variable named in $a.
Don't use this syntax in your own code.
$$a is a variable which name is in $a
Assuming $a = "foo";, $$a will be same as $foo
In PHP each variable starts with an $.
So for example you have the variable $a = 'var';
So $$a == $var
This new variable will have the "content" of the other variable as name.

PHP's =& operator

Are both these PHP statements doing the same thing?:
$o =& $thing;
$o = &$thing;
Yes, they are both the exact same thing. They just take the reference of the object and reference it within the variable $o. Please note, thing should be variables.
They're not the same thing, syntactically speaking. The operator is the atomic =& and this actually matters. For instance you can't use the =& operator in a ternary expression. Neither of the following are valid syntax:
$f = isset($field[0]) ? &$field[0] : &$field;
$f =& isset($field[0]) ? $field[0] : $field;
So instead you would use this:
isset($field[0]) ? $f =& $field[0] : $f =& $field;
They both give an expected T_PAAMAYIM_NEKUDOTAYIM error.
If you meant $o = &$thing; then that assigns the reference of thing to o. Here's an example:
$thing = "foo";
$o = &$thing;
echo $o; // echos foo
$thing = "bar";
echo $o; // echos bar
The difference is very important:
<?php
$a = "exists";
$b = $a;
$c =& $a;
echo "a=".$a.", b=".$b.", c=".$c."<br/>"; //a=exists b=exists c=exists
$a = null;
echo "a=".$a.", b=".$b.", c=".$c; //a= b=exists c=
?>
Variable $c dies as $a becomes NULL, but variable $b keeps its value.
If you meant thing with a $ before them, then yes, both are assigning by reference. You can learn more about references in PHP here: http://www.php.net/manual/en/language.references.whatdo.php
Yes, they do. $o will become a reference to thing in both cases (I assume that thing is not a constant, but actually something meaningful as a variable).

Categories