$bookA = "123";
$crack = "A";
I want to do something similar to this:
echo $book$crack;
Such that the output is 123.
What is the correct syntax for the echo command?
Thanks.
echo ${"book" . $crack};
These are called variable variables, but you should use arrays instead.
$varname = 'book'.$crack;
echo $$varname;
You might want to use an associative array.
For instance:
$book = array();
$book["A"] = "Some Book";
$crack = "A";
//Later
echo $book[$crack];
This will work:
$bookA = "123";
$crack = "A";
$var = "book$crack";
echo $$var;
Try the following:
echo ${book.$crack};
It works for me.
Related
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;
}
I have a variable ($uid) which fetches value from a database. My problem is that I am not able to append that variable to another variable ($str). The value of the $uid will be in place of '___' in the $str variable. How do I do approach?
$uid = 5;
$str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';
//use this code
$uid = 5; $str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';
echo str_replace("___",$uid,$str);
try this
$uid = 5;
$str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';
echo str_replace("___",$uid,$str);
output : ALPHA|5|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA
What a short way to do something like this?
$variable = array($variable);
To avoid repeating $variable
you could make your own function that takes $variable by reference.
function makeArray(&$var){
$var = array($var);
}
$test = "whatever";
makeArray($test);
var_dump($test);
$variable = (array) $variable;
I'm having some issues with the StdClass() object in PHP..
I'm using it to send information (a string and boolean) to a function.
Outside the function, it works great.
$args = new StdClass();
$args->str = "hej";
$args->ic = TRUE;
fun($arg);
This is then the function called:
function fun($args) {
$str = $args->str;
$ignore_case = $args->ic;
echo $str;
echo $ignore_case;
}
which just writes "stric" instead of the variable contents.
Is there a way to use StdClass to transfer this data and read it correctly?
//Martin
function fun($args) {
$str = $args->str;
$ignore_case = $args->ic;
echo $str;
echo $ignore_case;
}
add $ and second echo should be $ignore_case - I believe
$args = new StdClass();
$args->str = "hej";
$args->ic = TRUE;
fun($arg);
Where is $arg defined? Your call should be fun($args).
You forgot the $ before the variable names in your echos.
echo $str;
echo $ignore_case;
Also, fun($arg); should be fun($args);
So i want to deo something like this and not sure how
for($s=0; $s < 5; $s++ ){
$pre_config_query = "select * from preconfig where code = '{$industry_string}_{$s}_{$class_string}'";
$pre_config_station = mysql_query($pre_config_query);
$it_exists = mysql_num_rows($pre_config_station);
if($it_exists>0){
$pre_config = mysql_fetch_assoc($pre_config_station);
$pre{$s} = $pre_config['id'];
I want the end product to have these 5 variables named
print $pre1;
print $pre2;
print $pre3;
print $pre4;
print $pre5;
That have the $pre_config['id'] if present....any ideas
You can use variable variables to accomplish that.
First, define a variable with the desired name:
$varname = "pre$s";
Second, assign a value to it:
$$varname = $pre_config['id'];
That's all!
this works but I'm not sure I'm answering your question.
<?php
for($s=1; $s < 6; $s++ ){
$it_exists=1;
if($it_exists > 0){
$pre_config = array('id'=>rand(10,99));
${"pre".$s} = $pre_config['id'];
}
}
echo $pre1."<br/>";
echo $pre2."<br/>";
echo $pre3."<br/>";
echo $pre4."<br/>";
echo $pre5."<br/>";
?>