PHP close tag concatenate confusion - php

eval("\$data = $myvar('https://www.example.com/json/id='". $_GET['name'] ."'))';
This got me an error, how to concatenate it properly?

Eval is dangerous, but for your specific question above, the quotes were off at the end, and myvar is supposed to be a function. See below:
eval('$data = myvar("https://www.example.com/json/id='. $_GET['name'] .'");');
If you use double quotes like "$data" then $data will first be evaluated and the result or value will be eval'd instead. This is one of the risks of using eval(). If you use double quotes, then escape the $ signs like so:
eval("\$data = myvar('https://www.example.com/json/id=". $_GET['name'] ."');");
Demo: IDEOne

eval("\$data = $myvar('https://www.example.com/json/id=". $_GET['name']."');");

Related

Using PHP variable in system() function

I am currently using this command:
system('"C:/xampp/htdocs/csv/txtfiles/PSPPfile.txt"');
I want to having something like with having variable inside, for instance:
$file='txtfiles/PSPPfile.txt';
system('"C:/xampp/htdocs/csv/$file"');
Something like above, kindly help me out. Thanks!
Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted.
In other words, youre assigning the argument to system to read $file as is, not as what the variable contains.
Use double quotes to assert that variable gets replaced - or perform string concatenation :
system( 'c:/xampp/htdocs/csv/' . $file );
$file = 'txtfiles/PSPPfile.txt';
system('C:/xampp/htdocs/csv/' . $file);

eval() function in php not working

i have a little problem with eval.
here is my code:
$postbit = $template->output('posts_list_index'); // output string: $postslist it's ok
eval('echo $postbit;'); // output string: $postslist (?)
Thanks :)
I think in eval() function you can able to replace string with values. Please refer http://in3.php.net/eval.
and the solution for your's is:
eval("$postbit = ".$template->output('posts_list_index'."); ");
echo $postbit ;
try this one.. I am not sure about it.
Javadewd's Answer is correct. I prefer using double quotes instead of single quotes. PHP Manual states
Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Eval requires that you escape your $:
eval('echo \$postbit;');

string literals in PHP

in C# you can declare a literal string that you don't expect to have any escaping in it like:
string myString = #"This\is\some\string\";
For PHP is there anyway to declare a string literal in this way without needing to escape every slash? I know someone out there will ask me "what have you tried?" so for the sake of completeness I will list what doesn't work:
$myString = "This\is\some\string\";
$myString = 'This\is\some\string\';
Use single quotes and you should be good (less single quotes of course). If you really don't want to have to escape anything try nowdoc.
EDIT: assuming PHP >=5.3 (thank you Sammitch)
as sequoia mcdowell pointed out heredocs/newdocs are the answer. like so:
$myString = <<<'EOT'
This\is\some\string\
EOT;

Using a variable in $html = file_get_html();

i can't understand why variables are not working here. Here is the code:
include_once('../simple_html_dom.php');
$url = htmlentities($_GET['q']);
$urlall = str_replace(" ", "+", $url);
$html = file_get_html('http://www.example.com/some?key=$urlall&hl=en');
echo $html->plaintext;
if you look at this code you will found $urlall variable which i have applied in the web address, but this variable can't extract its data. As a new PHP programmer can't understand what to do now to make it works.. Here i have used HTML DOM PARSER..Thanks
Strings inside single quotes are literal, so $urlall is just a string, it won't be replaced with accual variable value. What you want to do is to use double quotes:
$html = file_get_html("http://www.example.com/some?key=$urlall&hl=en");
For futher explanation refer to PHP Strings:
Note: Unlike the double-quoted and heredoc syntaxes, variables and
escape sequences for special characters will not be expanded when they
occur in single quoted strings.
PHP Variables are not parsed and replaced in single quotes.
Try
$html = file_get_html("http://www.example.com/some?key=$urlall&hl=en");
Try to replace single quotes with double:
"http://www.example.com/some?key=$urlall&hl=en"
or use string concatenation instead of direct variable input into string:
'http://www.example.com/some?key='.$urlall.'&hl=en'
Second variant is more preferable.
I hope this helps you

Working with a Variable in PHP

Please tell me what I am doing wrong, when the php file executes is saves the actual folder as "$name" instead of Peter. What am I doing wrong?
Here is my code:
$name = "Peter";
copy_directory('you/','dir/$name');
You'll need to use double quotes in order for the variable to be interpreted as Peter
copy_directory('you/',"dir/$name");
You need to use double quotes if you want to expand variables within a string.
$name = "Peter";
copy_directory('you/',"dir/$name");
Or, alternately, concatenate the variable onto the string:
copy_directory('you/','dir/' . $name);
Use double quotes instead of single quotes;
$name = "Peter"; copy_directory('you/',"dir/$name");
Or alternatively, concatenate the variable;
$name = "Peter"; copy_directory('you/','dir/' . $name);
It's good practice to use concatenation operator while using php variables.
copy_directory('you/','dir/'.$name);
Updated Answer:
This could be big debate what to use. It's everyone's own opinion. But people say we should avoid complexity of double quotes. Double quotes have memory save issues. It doesn't matter for small values.
So I thought it's good practice to use concatenation operator while using php variables.
link1
link2
link3
link4
link5
link6
Problem is that you use the ' but should use there "" or 'dir/'. $name:
copy_directory('you/','dir/$name');

Categories