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

Example is a variable declaration within a function:
global $$link;
What does $$ mean?

A syntax such as $$variable is called Variable Variable.
For example, if you consider this portion of code:
$real_variable = 'test';
$name = 'real_variable';
echo $$name;
You will get the following output:
test
Here:
$real_variable contains 'test'
$name contains the name of your variable: 'real_variable'
$$name mean "the variable thas has its name contained in $name"
Which is $real_variable
And has the value 'test'
EDIT after #Jhonny's comment:
Doing a $$$?
Well, the best way to know is to try ;-)
So, let's try this portion of code:
$real_variable = 'test';
$name = 'real_variable';
$name_of_name = 'name';
echo $name_of_name . '<br />';
echo $$name_of_name . '<br />';
echo $$$name_of_name . '<br />';
And here's the output I get:
name
real_variable
test
So, I would say that, yes, you can do $$$ ;-)

The inner $ resolves the a variable to a string, and the outer one resolves a variable by that string.
So, consider this example
$inner = "foo";
$outer = "inner";
The variable:
$$outer
would equal the string "foo"

It's a variable's variable.
<?php
$a = 'hello';
$$a = 'world'; // now makes $hello a variable that holds 'world'
echo "$a ${$a}"; // "hello world"
echo "$a $hello"; // "hello world"
?>

It creates a dynamic variable name. E.g.
$link = 'foo';
$$link = 'bar'; // -> $foo = 'bar'
echo $foo;
// prints 'bar'
(also known as variable variable)

I do not want to repeat after others but there is a risk using $$ :)
$a = '1';
$$a = 2; // $1 = 2 :)
So use it with head. :)

It evaluates the contents of one variable as the name of another. Basically it gives you the variable whose name is stored in $link.

this worked for me (enclose in square brackets):
$aInputsAlias = [
'convocatoria' => 'even_id',
'plan' => 'acev_id',
'gasto_elegible' => 'nivel1',
'rubro' => 'nivel2',
'grupo' => 'nivel3',
];
/* Manejo de los filtros */
foreach(array_keys($aInputsAlias) as $field)
{
$key = $aInputsAlias[$field];
${$aInputsAlias[$field]} = $this->request->query($field) ? $this->request->query($field) : NULL;
}

Related

Check whether variable with the name of array value is set or not

So I have this array:
$myArray = array('myVar', 'myVar75', 'myVar666');
How do I isset check whether the variable called $myVar,$myVar75,$myVar666 exists or not?
What is the most sensible way of passing the array's value into the isset() function as a variable name to check?
Just use variable variables to test each element with a simple foreach loop:
Example:
$myVar666 = 1; // for example's sake
$myArray = array('myVar', 'myVar75', 'myVar666');
foreach($myArray as $element) {
if(isset(${$element})) {
echo $element, ' is already set';
} else {
echo $element, ' is not yet set';
// if not set, do something here
}
}
Should yield something like this:
$myVar is not yet set
$myVar75 is not yet set
$myVar666 is already set
foreach($myArray as $value){
if(isset($$value)){
echo "$value is exist with value : '".$$value."'";
}
else{
"$value is not exist";
}
}
You'd do that by putting an extra $, as follows:
$myArray = array("myVar", "myVar75", "myVar666");
if (isset($$myArray[0])) {
//Content
}
That would check if $myVar is set. Change the index to myArray accordingly.
The way to think about PHP's implementation of this (called variable variables) is that the content of the string goes after the dollar sign:
$hi = "hello";
But if you put $$hi, then the $hi is replaced with the string's content, so it becomes $hello.
$hi = "hello";
$hello = "greetings";
If you put $$$hi, then the $hi is replaced so that it becomes $$hello, and the $hello is replaced so that it becomes $greetings.

check a php variable with a generic function that will work with any variable

I'm trying to make a PHP function that wraps around a variable that will check the variable value and change it if it matches another variable.
I'm sure I'm doing it wrong, but...
Here's what I have so far:
<?php
function Clear_Value(){
$val='NONE';
if(this== $val){ this=='';}
};
$one = 'One';
$two = 'Two';
$three = 'NONE';
$four = 'Four';
Clear_Value($one);
Clear_Value($two);
Clear_Value($three);
Clear_Value($four);
echo $one.'<br>';
echo $two.'<br>';
echo $three.'<br>';
echo $four.'<br>';
?>
The output I'm going for would be:
One
Two
Four
I hope that's clear. I'm still learning functions in php so any pointers would be great.
Thanks
You need to pass an argument by reference:
function Clear_Value(&$arg){
if ($arg == 'NONE') $arg = '';
}
This way, the function can modify the variable's contents.
Live example: http://ideone.com/igHc5
what your constant this stands for?
you have to create some input variable in your function and use & prefix (refers to variable in memory)
function Clear_Value(&$var){
$val='NONE';
if($var == $val)
$var = '';
};
and another thing, if you want to change value of a variable, you use just single "=". i recommend you to see basics of syntax at php.net
sorry for my english
I am not sure what you expect here.
But when you execute this program it will show undefined variable error.
Try my sample changed code:-
<?php
function Clear_Value($sam){
$val='NONE';
if($sam== $val){ $sam=='';}
echo $sam.'<br>';
};
$one = 'One';
$two = 'Two';
$three = 'NONE';
$four = 'Four';
Clear_Value($one);
Clear_Value($two);
Clear_Value($three);
Clear_Value($four);
/*
echo $one.'<br>';
echo $two.'<br>';
echo $three.'<br>';
echo $four.'<br>';
*/
?>

PHP - How can I use curly bracket with variable correctly?

I read php document and I saw this:
class foo{
var $bar = 'I am a bar';
}
$foo = new foo();
$identity = 'bar';
echo "{$foo->$identity}";
And I saw somebody wrote like this:
if (!isset($ns->job_{$this->id})){
//do something
}
But when I tried with this code, It didn't work:
$id1 = 10;
$no = 1;
echo ${id.$no};
Can you guys tell me why it didn't work and when I can use braces with variable correctly?
Live example
Brackets can be used on object types, for instance, to simulate a array index. Supposing that $arr is an array type and $obj an object, we have:
$arr['index'] ===
$obj->{'index'}
You can make it more fun, for instance:
$arr["index{$id}"] ===
$obj->{"index{$id}"}
Even more:
$arr[count($list)] ===
$obj->{count($list)}
Edit: Your problem --
variable of variable
// Your problem
$id1 = 10;
$no = 1;
$full = "id{$no}";
var_dump($$full); // yeap! $$ instead of $
What are you expecting?
$id = 10;
$no = 1;
echo "${id}.${no}"; // prints "10.1"

Variable variables in OOP

I'm having a little struggle on this one and would appreciate some help.
In PHP variable variables can easily be defined like this
$a = "myVar";
$$a = "some Text";
print $myVar; //you get "some Text"
Now, how do I do that in a OOP enviroment? I tried this:
$a = "myVar";
$myObject->$a = "some Text"; //I must be doing something wrong here
print $myObject->myVar; //because this is not working as expected
I also tried $myObject->{$a} = "some Text" but it's not working either. So I must be very mistaken somewhere.
Thanks for any help!
This works for me:
class foo {
var $myvar = 'stackover';
}
$a = 'myvar';
$myObject = new foo();
$myObject->$a .= 'flow';
echo $myObject->$a; // prints stackoverflow
This should work
class foo {
var $myvar = 'stackover';
}
$a = 'myvar';
$myObject = new foo();
$myObject->$a = 'some text';
echo $myObject->myvar;

Is there a way to bind variables? PHP 5

Using PHP 5 I would like to know if it is possible for a variable to dynamically reference the value
of multiple variables?
For example
<?php
$var = "hello";
$var2 = " earth";
$var3 = $var.$var2 ;
echo $var3; // hello earth
Now if I change either $var or $var2 I would like $var3 to be updated too.
$var2 =" world";
echo $var3;
This still prints hello earth, but I would like to print "hello world" now :(
Is there any way to achieve this?
No, there is no way to do this in PHP with simple variables. If you wanted to do something like this in PHP, what you'd probably do would be to create a class with member variables for var1 and var2, and then have a method that would give you a calculated value for var3.
This should do the trick. I tested it on PHP 5.3 and it worked. Should also work on any 5.2.x version.
You could easily extend this with an "add"-Method to allow an arbitrary number of strings to be placed in the object.
<?php
class MagicString {
private $references = array();
public function __construct(&$var1, &$var2)
{
$this->references[] = &$var1;
$this->references[] = &$var2;
}
public function __toString()
{
$str = '';
foreach ($this->references as $ref) {
$str .= $ref;
}
return $str;
}
}
$var1 = 'Hello ';
$var2 = 'Earth';
$magic = new MagicString($var1, $var2);
echo "$magic\n"; //puts out 'Hello Earth'
$var2 = 'World';
echo "$magic\n"; //puts out 'Hello World'
No. Cannot be done without utilizing some sort of custom String class.
Check the PHP manual for types and variables, especially this passage:
By default, variables are always
assigned by value. That is to say,
when you assign an expression to a
variable, the entire value of the
original expression is copied into the
destination variable. This means, for
instance, that after assigning one
variable's value to another, changing
one of those variables will have no
effect on the other. For more
information on this kind of
assignment, see the chapter on
Expressions.
it's a bit late, but it's interesting question.
You could do it this way:
$var = "hello";
$var2 = " earth";
$var3 = &$var;
$var4 = &$var2;
echo $var3.$var4; // hello earth
From my point of view the following code is a little closer to the required:
$a = 'a';
$b = 'b';
$c = function() use (&$a, &$b) { return $a.$b; };
echo $c(); // ab
$b = 'c';
echo $c(); // ac
Create a function.
function foobar($1, $2){
$3 = "$1 $2";
return $3;
}
echo foobar("hello", "earth");
echo foobar("goodbye", "jupiter");

Categories