Is it possible to have a variable in a session name?
Like if I have a variable: $id
I want something like this:
$_SESSION['number'$id]
So that if $id=1
$_SESSION['number1']
And if $id=65
$_SESSION['number65']
Yes - you can do this.
All you have to do is concatenate a string value as the index in the $_SESSION array.
$id= 42;
$result = $_SESSION['number'.$id];
Now $result will be equal to the value of $_SESSION at the index "number42";
The same can be done for any associative array as well.
$any_assoc_array = array('index42'=>'Hooray!');
$id= 42;
$result = $any_assoc_array['index'.$id];
echo $result;
OUTPUT :
Hooray!
In PHP you can place variable names within a string literal if in double-quotes, so the following is perfectly legal. Note that the variable can appear anywhere in the string literal.
$result = $_SESSION["number$id"];
Related
Say for example that I have this variable
$p0001 = array("title"=>"This is the title","name"=>"Just Me");
And the URL is
https://www.example.com?id=p0001
How do I get the correct array from PHP? I've tried
echo $_GET["id"]["title"];
To explain little more, say I have two array variables. I want it to echo the "title" from from the array $p0001. So how do I make sure I gets the variable I put in $_GET?
You can use Variable variables
So, your variable name is $p0001. Your GET parameter id is basically a pointer to the variable name, so using variable variables, we can reference the variable we're looking for:
$varname = $_GET['id']; // $varname = 'p0001';
$$varname; // this is basically $p0001
$$varname['title']; // and you can get your title from $p0001
You could also check if a variable exists before using with isset($$varname)
$body->append('{"stage_id": "43"}');
I would like to make the "43" dynamic and use a $variable for the value: How should look the Syntax look like?
You can use just like this:
<?php
$var = 43;
$array = array('stage_id'=>$var);
$json = json_encode($array);
$body->append($json);
?>
This will help you
$body->append('{"stage_id": $variable}');
First of all you have to declare a variable and assign your dynamic value in it as per your requirement.
$variable = Value; // if value is number like 0,1,2,3,4 or anything.
$variable = "value"; // if value is string put it inside ' or " quotes.
Now you can use your value as a variable:
body->append("{'stage_id': '$variable'}");
I'm trying to work out how to get values from one of three arrays based on the array name.
$ABC001 = array('A'=>'10','B'=>'2','C'=>'1.0');
$ABC002 = array('A'=>'20','B'=>'4','C'=>'1.1');
$ABC003 = array('A'=>'30','B'=>'6','C'=>'1.2');
I have a variable passed to my script it will be contain something like ABC#001 or ABC#002
I'm removing the # so the var value now matches the array name/
$test = str_replace('#','',$var);
If I do var_dump ( $$test ) I get all the values from the correct array, but if I do echo $$test['A'] or echo $$test[0] I don't get the value from the first key in the correct array.
Can someone advise how to do this.
Thanks
try this ${$test} to get the values of the array
<?php
$ABC001 = array('A'=>'10','B'=>'2','C'=>'1.0');
$ABC002 = array('A'=>'20','B'=>'4','C'=>'1.1');
$ABC003 = array('A'=>'30','B'=>'6','C'=>'1.2');
$var = "ABC#002";
$test = str_replace('#','',$var);
var_dump(${$test}['A']);
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$test['A'] then the parser needs to know if you meant to use $test['A'] as a variable, The syntax for resolving this ambiguity is: ${$test}['A'] . Please check the documention here PHP Variable Variable
We know we can't store a function in variable
<?php
$k = echo("hello");
?>
or
$k = unset($_SESSION['name']);
They all will give error. because we can't store a function in variable directly, but how we can use.
$row = mysqli_fetch_assoc($result);
I know mysqli_fetch_assoc() function output an array which is stored in variable $row . but how a general rule of php is violated. and we are able to store function in a variable
echo and unset are not really functions. If you check the documentation, you'll see that it says:
Return Values
No value is returned
Since no value is returned, you can't assign them to variables.
mysqli_fetch_assoc() is an ordinary function. And like any other function, you can assign its return value to a variable, or pass it as an argument to another function, or use it in an expression.
You would have a hard time programming in PHP if you couldn't assign function values to variables. You couldn't write things like:
$max = max($var1, $var2);
$current_time = time();
We know we can't store a function in a variable
This is not true, but I'll get back to it later.
You are misunderstanding the syntax below:
$variable = functionName();
This does not mean that the function is stored in a variable. It means that the value returned by the function is stored in the variable.
So there is nothing special about:
$row = mysqli_fetch_assoc($result);
You should also know as I hinted at in the beginning, that we can in fact store a function in a variable:
//store the function in variable $func
$func = function($a, $b){return $a + $b;}
//execute the function stored in variable $func
echo $func(2,8); //prints 10
These types of functions -- functions without names -- care called anonymous functions or closures. See the manual
I am not getting any results while passing the values intot the stores procuder I have the code below
<?php
$a="26456";
$result3=mysql_query('CALL getItemStock($a)');
$row = mysql_fetch_array($result3);
?>
Try this:
$result3=mysql_query('CALL getItemStock('.$a.')');
or
$result3=mysql_query("CALL getItemStock($a)");
UPDATE:
If the parameter defined as string then you need to enclose it with quotes as well. For example:
$a = 'I am String';
$result3=mysql_query("CALL getItemStock('$a')");
However, if the parameter defined as number, then no quotation required.