how can we inject a PHP variable into Javascript - php

I have a problem with the "windows.location" command in JavaScript. I would like to add the php variable in the link windows.location. How can i do?
For example: I would like to transfer user to English page or Vietnamese Page by variable $lang
Here is my code
echo 'window.location="/B2C/$lang/confirm_fone.html"';
and the result in address bar is:
http://10.160.64.4:1234/B2C/$lang/confirm_fone.html
the $lang in address bar cannot be decode?

Variables in single-quoted strings don't get interpolated in PHP.
Use this instead:
echo 'window.location="/B2C/' . $lang . '/confirm_fone.html"';
Or use doublequotes:
echo "window.location='/B2C/$lang/confirm_fone.html'";

This is because the whole string is in single quotes.
You'll want to use double quotes for interpolation.
Otherwise, you can try:
echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"';

If you put php variables within the string you should use Double Quotes .
echo "window.location='/B2C/$lang/confirm_fone.html'";

You have to concatenate the value, as follows :
echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"';

Variables are not resolved by PHP in Strings when you use the ' as delimiter. Use " instead (and ' for the javascript command) or concatenate the String using ..

If you are in php-context:
echo "window.location=\"/B2C/"{$lang}"/confirm_fone.html\";';
If you are in HTML-Context (or better "outer-php-context"):
window.location="/B2C/<?php echo $lang ?>/confirm_fone.html";

Related

How to print out the name of the variable and its value to browser?

I have just started learning PHP, so please forgive me for asking a question like this.
I have a variable $myVar = "hello"; and I want to print out the name of the variable and its value to the browser.
I tried
echo "$myVar: $myVar"; //this simple prints out the value of $myVar twice
also tried
echo '$myVar'+ ":$myVar"; //somehow the number 0 was printed out to the browser
also tried
echo '$myVar: $myVar'; //this just prints out the string literal itself
I understand the difference between double quotes and single quotes, but just can't seem to get this simple example working.
You can simply do this:
echo "\$myVar: $myVar";
Use backslash \ to display the variable name as it is.

PHP add string to first and last

How to add " string into the first and last in PHP,
something like this :
hello world
into like this :
"hello" "world"
is there before-string and after-string function in PHP?
In PHP there are two basic ways to get output: echo and print.
In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.
For more datailes use http://www.w3schools.com/php/php_echo_print.asp
Simply use it like as
echo '"Hello" "World"';
You can simply use preg_replace like as
echo preg_replace('/(\w+)/',"\"$1\"","hello world");
Demo
try this:
<?php
$str="hello world";//your string
$str=str_replace(" ",'" "',$str); //replace all spaces with " "
echo '"'.$str.'"';//while displaying add double quotes in begning and end.
?>
There is no such function.
However you can try using split function on space of the string, then use implode() function with your glue or you can simply try using regex.Latter would give you a 1 line call to do this.

How to pass string variable to php function?

I am using exec() to execute the file. One of the argument I need to pass is string.
shell_exec('/home/technoworld/Videos/LinSocket/client "Hi hello"'); This works fine!
But when I take string into var i.e. $s="Hi hello" and
'shell_exec('/home/technoworld/Videos/LinSocket/client . "$s"')`. It does not works. If
'shell_exec('/home/technoworld/Videos/LinSocket/client . $s')' goes in infinite wait!
Any Idea how to pass $s to the function?
shell_exec('/home/technoworld/Videos/LinSocket/client "'.$s.'"');
or use double quotes to parse variables
shell_exec("/home/technoworld/Videos/LinSocket/client '$s'");
or
shell_exec("/home/technoworld/Videos/LinSocket/client \"$s\"");
Your best bet is to use the escapeshellarg command
shell_exec('/home/technoworld/Videos/LinSocket/client '.escapeshellarg($s));

Adding PHP code in HTML which is already in PHP? [duplicate]

This question already has answers here:
Simple PHP stuff : variable evaluation [duplicate]
(5 answers)
Closed 11 months ago.
Basically, I want to add a PHP variable in HTML, with that HTML already being inserted into a constant, which is in PHP code. Here's what I mean: (obviously this code below is wrong, but imagine I would want to be inserting the $VARIABLE in the URL of the iFrame, for example)
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/$VARIABLE'></iframe>";
?>
What would be the syntax for adding that variable in there?
Thanks in advance!
This is just a basic template. str_replace() should do the trick.
const EXAMPLE = "<iframe src='http://example.com/{{{VARIABLE}}}'></iframe>";
$variable = 'example-sub-category
$merged_content = str_replace('{{{variable}}}', $variable, EXAMPLE);
Note I used {{{}}} to denote the insert. This is not PHP syntax, but you will find templates often use something like that the would not be expected in the text otherwise to denote placeholders.
Have you tried:
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/".$VARIABLE."'></iframe>";
?>
You can insert PHP anywhere in HTML just by defining its tags <?php ?>
<iframe src='http://example.com/<?php echo $VARIABLE; ?>'></iframe>
Using your example:
const EXAMPLE = "<iframe src='http://example.com/" . $VARIABLE . "'></iframe>";
For this kind of task I'd better use sprintf:
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/%s'></iframe>";
echo sprintf(self::EXAMPLE, $VARIABLE);
To move in and out of php within your html, use <? [php code here...] and ?> to end php and continue with html. You can escape characters with a back slash \ and using single quotes as Rujikin mentioned above. Or, within PHP, you can echo your html so that you're not bouncing in and out of php.
For example:
some php...
echo '<span style=\"color:#980000\"><strong>\"{$searchTerm}\"</strong></span>';
more php...
Notice that the html is enclosed in single quotes (end-to-end) and inside the single quotes, double quotes are used where required by html, e.g., for the <style> tag.
This example is basically saying, "echo whatever the value of php variable $searchTerm is, put it in quotes that's required by echo, and make it dark blue (#0000FF) and bold (<strong>).
I hope this helps. :)
You can try something like:
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/$VARIABLE'></iframe>";
$newHtml = str_replace('$VARIABLE', $VARIABLE);
?>
As you mentioned your code was fake, so I guess what you need is to replace some places in your HTML strings by a content which is unknown until you php script is executed, right ?
so what you need is to put some place holders and change them later, as I showed in my snippet above.
Hope this works

php combining url with variable

I want to do define the following variable $url
$url = www.example.com/$link;
where $link is another predefined variable text string e.g. testpage.php
But the above doesn't work, how do I correct the syntax?
Thanks
Try this:
$url = "www.example.com/$link";
When string is in double quotes you can put variables inside it. Variable value will be inserted into string.
You can also use concatenation to join 2 strings:
$url = "www.example.com/" . $link;
Hate to duplicate an answer, but use single quotes to prevent the parser from having to look for variables in the double quotes. A few ms faster..
$url = 'www.example.com/' . $link;
EDIT: And yes.. where performance really mattered in an ajax backend I had written, replacing all my interpolation with concatenation gave me a 10ms boost in response time. Granted the script was 50k.
Needs double quotes:
$url = "www.example.com/$link";
Alternate way:
$url = "www.example.com/{$link}";
$url = "www.example.com/$link";
It'd be helpful if you included the erroneous output, but as far as I can tell, you forgot to add double quotes:
$url = "www.example.com/$link";
You will almost certainly want to prepend "http://" to that url, as well.

Categories