Dynamic Location header PHP [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is PHP not replacing the variable in string?
I would like to have my PHP redirect headers dynamic. Currently I have:
header('Location:message.php?ref=conversation&conv=$conv_id')
But that redirects the user to:
doamin.com/message.php?ref=conversation&conv="$conv_id"
What am I doing wrong?

You are putting a variable in single quotes, change to double quotes.

Change it into this:
header("Location:message.php?ref=conversation&conv=$conv_id");
Or this:
header('Location:message.php?ref=conversation&conv=' . $conv_id);
(I prefer the latter, but that's just a personal thing.)
The thing is that in php, it parses variables inside a string if you use double quotes and doesn't if you use single quotes.

anything in single quote behave like a string
so
header('Location:message.php?ref=conversation&conv=$conv_id') will not work
should be
header("Location:message.php?ref=conversation&conv=$conv_id")

Not much, you should pass the var into the string
header("Location:message.php?ref=conversation&conv=" . $conv_id)

You can do one of these two:
header("Location:message.php?ref=conversation&conv=$conv_id");
OR
header('Location:message.php?ref=conversation&conv='.$conv_id);
you shouldn't place the variable in the single quote '. place it in the double " quote, will work

Related

How to escape quotes in a string that assigns a method to onclick with a parameter? [duplicate]

This question already has answers here:
Single and double quotes together as HTML attribute value?
(2 answers)
Closed 3 years ago.
I have a string that contains HTML-Code. And within that string I am assigning a method WITH AN ARGUMENT to the onclick-attribute. This attribute needs to be a string though.
This is the code.
This is how it looks like in HTML.
This is how it should look like in HTML.
I tried to put double quotes before and after, escape them, escape them with multiple backslahes, and many other things, nothing works. All ideas are appreaciated. :-)
This looks like a job for stripslashes(). Note however, that you can not use the same double quote (") or single qoutes (') for both the attribute assignment AND the string definition for the parameter. Therefore you want your result to look something like: onclick="removeTag('naruto')".
If you have them as double quotes in your original string (i.e. $row['description']), you can replace those, all put together like this:
echo 'blabla <a href="#" onclick="removeTag(' . str_replace('"', '\'', stripslashes($row['description'])) . ')">';
A tiny bit simpler approach might be to just trim double quotes and add your own single qoutes:
echo 'blabla <a href="#" onclick="removeTag(\'' . trim(stripslashes($row['description']), '"') . '\')">';

How to fix double/single quotes in PHP [duplicate]

This question already has answers here:
How to work with double quotes and single quotes in php [duplicate]
(2 answers)
Closed 3 years ago.
this should be an easy question but im finding it hard to get a working answer, i am using Sendgrid php api and it requires that i do the following code in this layout.
in my code i have:
//this part is the sendgrid part:
$email->addContent(
"text/html", "//i need to put the bottom image code into here"
<?php echo '<img src="data:image/png;base64,'.base64_encode($portal['image']).'"width="1024px" height="768px"/>'; ?>
the end result should look something like:
$email->addContent(
"text/html", "<img src="data:image/png;base64,'.base64_encode($portal['image']).'"width="1024px" height="768px"/>"
i cant figure out how to use the correct quotes so that this code will work since it uses double and single quotes inside of eachother
You have to match the starting and ending quotes for a string. You're starting the first string with ", but then ending it with ' before the concatenation ..
If you want to put double quotes inside the string, use single quotes around it, and vice versa. If you need to include the same kind of quote inside the string as you use two create it, you have to escape the inner one.
$email->addContent(
"text/html", '<img src="data:image/png;base64,'.base64_encode($portal['image']).'" width="1024px" height="768px"/>'

php - alternative for document.getElementById [duplicate]

This question already has answers here:
PHP quotes inside quotes
(3 answers)
Closed 5 years ago.
hi guys i just want to know if there is an alternative syntax for onclick="document.getElementById('id01').style.display='none'" on php. Beceause its not working even it is inside the echo.tnx
here is my code
First up: document.getElementById is a construct used in JavaScript and has nothing to do with PHP.
The problem you are running into is that you are not escaping the single quotes in your string:
echo '<button onclick="document.getElementById(' <- there is your problem
PHP thinks that the string ends here since you told it so with '.
But the string is in fact not at its end so you need to escape the single quote in order to make it work:
echo '<button onclick="document.getElementById(\'id0\')...';
This goes for all single quotes in your string of course.

single qoute syntax printing in php [duplicate]

This question already has answers here:
single quote inside double quote in php
(3 answers)
Closed 9 years ago.
The below outputs
href="javascript:showBed(" a114:1')'
when I want it on the form
href="javascript:showBed('A114:1')"
in order to get javascript to work. I had a look at this site but coudn't get it to work so I gave up. Perhaps you could give me a hint on how the corrent syntax would be?
echo("<a href='javascript:showBed('" . $row['Bed'] ."')' target='main' class='larmlink'>link</a>");
Thanks =)
Your output is not what it would output, but it is how it would be interpreted (HINT: don't look at a parsed DOM tree, look at the source).
echo("<a href='javascript:showBed('" . $row['Bed'] ."')' ...
==>
echo("<a href=\"javascript:showBed('" . $row['Bed'] ."')\" ...
You really should be using the more standard double quotes around HTML element properties. As such, it is probably best to use single quotes in PHP. I would suggest this:
echo('link');
To print the double-quote character, you can escape it by doing \"
echo("<a href=\"javascript:showBed('" . $row['bed'] ."')\" target='main' class='larmlink'>link</a>");
Live demo
When you want to output variable data to JavaScript, it is good to use json_encode() so that all special characters are escaped automatically. The htmlspecialchars() escapes any values for use in the HTML attribute value.
echo '<a href="',
htmlspecialchars('javascript:showBed(' . json_encode($row['Bed']) . ')'),
'" target="main" class="larmlink">link</a>';
Note that I use single quotes for PHP string literals so that PHP doesn't have to search through my string for a variable to replace. You don't have to do this, but I recommend it.
I like to use sprintf (or printf, but sprintf is easier to refactor) for long strings like this so it's easy to see the template:
echo sprintf("<a href='javascript:showBed(\"%s\")' target='main' class='larmlink'>link</a>", $row['Bed']);
I'd also consider using addslashes on the $row['Bed'] variable in case it has quotes in it.
Using the heredoc syntax often makes code with mixed quotes easier to understand:
echo <<<EOD
link
EOD;
As others mentioned, if the value of your $row['Bed'] might contain single or double quotes, you have to escape it with addslashes.
You can use the heredoc syntax to avoid to escape anything:
echo <<<LOD
link
LOD;
Notice that if your variables contains some quotes you must use the addslashes function or str_replace before.
Another good practive is to separate systematically all the html content from php code:
<a href="javascript:showBed('<?php
echo $row['Bed'];
?>')" target="main" class="larmlink">link</a>
try this one:
echo("<a href='javascript:showBed(\"" . $row['Bed'] ."\")' target='main' class='larmlink'>link</a>");

Difference between uses of inverted commas in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
Can you use " and ' interchangeably, 100%? Or is there a reason or use for each? What is the difference exactly?
Observe for yourself:
$name = 'John';
echo "Hello $name" . '<br>';
echo 'Hello $name';
Result:
Hello John // result from double quotes
Hello $name // result from single quotes
As can be seen variables inside double quotes are parsed while in single quotes they aren't.
So when you put variables inside double quotes, they can be parsed and their correct value is output whereas with single quotes, variables are not parsed and you get the same output of variable name itself as in Hello $name.
Since variables inside single quotes aren't parsed, using them is just a little good when it comes to performance.
If there is no question of variables inside quotes, you can use them inter-changeably though keeping above performance tip in mind.
For more information, you can look at the official documentation.
Just to add to the great answer of Sarfraz, there are certain situations where you would want to use one or the other.
Single quotes ('') are always parsed slightly (minutely) faster than double quotes so if you are an optimization freak, a good rule of thumb is to use single quotes instead of double quotes if you will not be parsing any variables.
However, if you have tons of variables and don't want to do something like:
echo 'My name is ' . $name . '!';
then you're better off with double quotes.
However when dealing with html output, you may consider the hassle of escaping your double quotes too tedious to deal with:
echo "<p id=\"myParagraph\">$name</p>";
So in this case the vote goes to single quotes.
Another thing is that when you build SQL queries with PHP, you may notice that you might prefer using double quotes to be able to parse variables and avoid escaping the single quotes:
"SELECT * FROM CoolGuys WHERE Name = '$name'";
In the end it's all a matter of preferrence. :)
Good luck!

Categories