Replace data dynamically from array in eval()? - php

I'm trying to replace the value from array.
$row= array("id"=>"35", "name"=>"test","first_name"=>"noor","last_name"=>"fathima");
// Eval Statement -
$row = $row['first_name'].' '.$row['last_name'];
eval("\$row = \"$row\";");
//This should return noor fathima
I am unable to replace the values. Can anyone please help me out?

Not a huge fan of eval() as it can be a pain, but the main thing is to get all of the right $'s and quotes escaped/unescaped etc. To get round this I've put the expression into single quotes as this stops any interpretation - until you eval() it...
$row= array("id"=>"35", "name"=>"test","first_name"=>"noor","last_name"=>"fathima");
eval('$result = $row["first_name"]." ".$row["last_name"];');
echo $result;
gives...
noor fathima

Why you need eval() here because it is already returning what you want as output?
<?php
$row= array("id"=>"35", "name"=>"test","first_name"=>"noor","last_name"=>"fathima");
echo $row['first_name'].' '.$row['last_name'];
?>
DEMO: https://3v4l.org/7KTH0

Related

Want Program logic in PHP To display string

I am having input as Word "CODE"
and I want to get output as "CCOCODCODE"
Please help me with the logic to print this output.
Thanks.
It's rather simple. Looking at your pattern, you need to concatenate all possible prefixes of CODE one after the other. So, maintain a result variable and keep concatenating substrings with 0 as the start point and end point being each index in the string.
<?php
$str = 'CODE';
$result = '';
for($i=0;$i<strlen($str);++$i){
$result .= substr($str,0,$i+1);
}
echo $result;
Try to avoid loops whenever you can.
Short and simple does the trick :-)
<?PHP
$Text = 'CODE';
echo implode(array_map(function($Position) use ($Text) { return substr($Text,0,$Position+1); },array_keys(str_split($Text))));

Echoing JSON Data in php

Im trying to echo some JSON Data. The problem is the data contains variables but my code isn't putting the variables into the string.
Heres my code:
$status = $row['Status'];
$priority = $row['Priority'];
echo '{"status":"$status","priority":"$priority"}' ;
this php is echoing
{"status":"$status","priority":"$priority"}
when I need to echo
{"status":"Completed","priority":"High"}
for example. How can I fix this?
Just use json_encode function
echo json_encode($row);
json_encode($row)
Will give you the desired output.
The problem here is that PHP does not substitute variables in single quotes, only in double quotes (see http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double).
For example:
$test = "a";
echo 'This is $test test and'.chr(10);
echo "this is $test test.".chr(10);
/*
Creates the following output:
This is $test test and
this is a test.
*/
Note: chr(10) creates the new line.
And the solution to your problem is to use json_encode() and json_decode() as other people have suggested already.
http://php.net/manual/en/function.json-encode.php
The problem is in your single quotes, PHP get all vars inside as strings, so break the string as follow:
echo '{"status":"'.$status.'","priority":"'.$priority.'"}' ;
On top of that, you can use json_encode() in order not to build your JSON object manually.

How to match a variable with value in double array

i have a problem like the following sample code :
$code='100'; //$maybe $code='0' or $code='1' ... i just set a number as an sample
$xx=array(
'0'=>array(a,b,c),
'1'=>array(d,e,f),
........
'100'=>array(aa,bb,cc)
);
I want find $code in array :
if($code==$xx['$code']){
echo $xx['code'][0]; //if i want get the value 'aa'
}
But it seems like $xx['$code'] doesn't work.
Do anyone know the right way to solve it?
Firstly you need to use array_key_exists for getting within if condition and then after you can use it simply like as
if(array_key_exists($code,$xx)){
echo $xx[$code][0];
}
or can simply use isset instead like as
if(isset($xx[$code])){
echo $xx[$code][0];
}
you shouldn't use single quotations here,
when filling displaying a variable either use without quotations or put it between double "" try $xx[$code] or $xx["$code"]
Use isset OR !empty to check key exist in array or not. It will also check that key has valid value.
if(isset($xx[$code])){
echo $xx[$code][0];
}
OR
if(!empty($xx[$code])){
echo $xx[$code][0];
}
if($code==$xx['$code']){
echo $xx[$code][0]; //if i want get the value 'aa'
}
If you use '$code' the content of $code will not be checked as '' interpret everything as a string and won't look a variables in it.
You can't sace 'code' either, as code is only the name of the variable you use.

PHP Output Analysis

<?php
$data="98.8degrees";
(double)$data;
(int)$data;
(string)$data;
echo $data;
?>
I was surprised/confused when the actual output was 98.8 degrees
I thought when $data uses (double), it converts to 98.8.
Then when moving to (int), it becomes 98 and forth
But I guess my analogy is wrong. Can someone explain to me how the output became like that?
Doing
(double)$data;
(int)$data;
(string)$data;
just return the double, int and string values but they don't change. To change them, you need to do assign the return values to the actual variable like this:
$data = (double)$data;
$data = (int)$data;
$data = (string)$data;
You keep casting it, but turn around and discard the results. The value in $data never changes. Try the settype() function.
What you probably meant to do was:
<?php
$data="98.8degrees";
$data=(double)$data;
$data=(int)$data;
echo $data;
?>
If you don't keep the result of your operations, you won't see the result of your operations...
Oh - and I have no idea what you hoped to achieve with the
(string)$string;
statement, since it's unclear if/how you ever defined $string...

Php variable in a variable

I have a piece of code that looks like this:
$result = mysql_query($queryc) or die(mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row['$field'];
}
}
Say that code was in a function and I wanted to pass $field to the $row[''] how would I accomplish that?
In other words I'm attempting to use $row['$field']; and $field is defined elsewhere
suppose you have this function definition:
function foo($field) {
// from that code you have given
....
echo $row[$field]; // no need of the quotation marks around $field
....
}
You'd not put any quotes around $field... like this:
echo $row[$field];
Variables are not expanded in single quotes; they are only expanded in double quotes or in the heredoc syntax:
When a string is specified in double quotes or with heredoc, variables are parsed within it.
So either use double quotes or, even better, just omit them:
$row[$field]
Single quotes inhibit variable substitution.
echo $row["$field"];
or just
echo $row[$field];
The latter is highly recommended as it does not require PHP to parse $row["$field"] into $row[$field]. Saves you some microtime in each iteration.

Categories