I ran into this code:
function input_text($elem, $val) {
print '<input type = "test" name="' . $elem .'" val="';
print htmlentities($val[elem]) . '"/>';
I m confused about the code: name="' . $elem .'" val="';
print htmlentities($val[elem]) . '"/>'
1) why put single quotes and dot inside double quotes around $elem? Can i just use double quotes like name="$elem".
2) what is the meaning of these code: val="';
print htmlentities($val[elem]) . '"/>'
1) Since the string being printed is surrounded with single quotes, variables are not expanded inside it; variables are only expanded inside double-quoted strings. So concatenation is necessary. If you change it to use double quotes, you could do variable interpolation:
print "<input type='test' name='$elem' val='";
2) There's no special meaning to it. The programmer simply chose to split up the commands to print this piece of HTML into two PHP print statements. So first he prints val=", then he prints htmlentities($val[elem]) . "">>'
The function could be rewritten as:
function input_text($elem, $val) {
print "<input type='test' name='$elem' val='" . htmlentities($val[elem]) . "'/>";
}
You have to use concatenation around htmlentities() -- only variables can be interpolated into strings, not function calls. However, you could assign the value to a variable first if you want:
function input_text($elem, $val) {
$valent = htmlentities($val[elem]);
print "<input type='test' name='$elem' val='$valent'/>";
}
BTW, $val[elem] looks like a typo, it probably should be $val[$elem].
The single quotes in this case denote a string in PHP.
$var = 'This is a String';
The reason they are used in conjunction with the double quotes is because the double quotes must be printed to get the correct HTML output of
<input type="test" name="someName" val="someValue" />
The . operator in PHP is the concatenation operator meaning combine 2 strings into 1.
$var = 'This' . ' and that'; //Evaluates to 'This and that'
Related
$playername = $_GET["playername"];
echo '<img src="https://cravatar.eu/head/SecretAgent5555"/>';
I need what SecretAgent5555 is to be $playername, plz help/
This will do what you need...
echo '<img src="https://cravatar.eu/head/' . $playername . '"/>';
This uses the PHP . string concatenation feature to 'insert' $playername into the img src tag.
You can also use string interpolation by using double quotes, but it requires you to escape the double quotes within, like this.
echo "<img src=\"https://cravatar.eu/head/$playername\"/>";
Because you have opened the string being echoed with the double quotes, to put a double quote within, you need to escape them. This is the \" part within the string.
I have these two lines that I would like to escape the $type variable:
$functionName = str_replace('-', '_', $type);
$output .= '<div class="tab-pane" id="'. $type .'">';
I tried escaping like below but its confusing me and not sure whether thats right:
$output .= '<div class="tab-pane" id="\'. $type .'\">';
Example 1: Variable between single quotes
If you use single quotes everything between them will always be treated as part of the string.
$output .= '<div class="tab-pane" id="' . $type . '">";
Example 2: Variable between double quotes (option 1)
If you have a variable that you want to pass in a string you can just put it in there if you use double quotes and de variable is nog 'touching' the other words. It should always have spaces.
$output .= "<p>i would like to $your_text_here with you.</p>";
Example 3: Escaping quotes in a string
Escaping characters in a string can be done by using a \ (backslash) before the character you want to escape.
$output .= "<div class=\"tab-pane\" id=\"example-id\">";
Example 4: Variable between double quotes without spaces next to it
You can place your variable between {} braces if you use double quotes (option 2)
$output .= "<div class=\"tab-pane\" id=\"{$type}\">";
This question was however already answered in Mixing PHP variable with string literal
Your first block is doing string replacements, but then you use the ORIGINAL string, not the replaced one:
$output .= '<div class="tab-pane" id="' . $functionName . '">';
would be more correct. On the second one, you're escaping the ' quotes, which means that you never terminate the string, meaning that the . $type . portion is treated as plaintext within the string, not a PHP concatenation operation. Try
$output .= '<div class="tab-pane" id="' . $type . '">';
instead. note the LACK of backslash escapes.
And of course, you could use a HEREDOC, eliminating any need to escape quotes entirely:
$output .= <<<EOL
<div class="tab-pane" id="{$functioName}">
EOL;
In this case, you don't need to escape at all. You only escape within the same type of quotes. You don't escape double inside single or single inside double.
So with 'o'reilly' you would escape like 'o\'reily'. But with "o'reilly" you'd just keep it as "o'reilly". But with "He said "hello"" you'd escape "He said \"hello\"". Yet, with 'He said "hello"' you would not escape at all.
But if your $type variable can contain double quotes, you will need to consider that to prevent your HTML from being broken in that case. How you would handle the quotes inside the variable $type would be by replacing the " with its HTML entity equivalent:
$output .= '<div class="tab-pane" id="' . str_replace('"', '"', $type) . '">';
Or use htmlentities() which will do the same replace as well as others.
Note, its the double quotes inside the variable you would want to handle, not to escape the single quotes outside. Because presumably the issue is that if the variable contained double quotes it would break your HTML since you are using double quotes around the value for id:
i.e. id="contents_of_type_variable"
If you had id="contents"_of_type_variable" your HTML would be broken.
So you change that to id="contents"_of_type_variable"
If you're trying to escape something else, it is due to a misunderstanding.
I can't find a solution how to combine single and double quotes to echo HTML and call a function at the same time:
foreach ($result as $r) {
echo "<a href='get_permalink(get_page($r->id))'>".get_permalink(get_page($r->id)).'</a><br>';
}
Problem is this part is parsed as text, not php
"<a href='get_permalink(get_page($r->id))'>"
Cansome one help me to combine this? get_permalinks and get page are wordpress built in functions, so they should have function behavior
You can't call a function inside double " quotes.
foreach ($result as $r)
{
echo "<a href='".get_permalink(get_page($r->id))."'>".get_permalink(get_page($r->id)).'</a><br>';
}
It's not possible to run PHP code when it's inside a string (unless with eval). However, you can use printf() to separate code from string:
$url = get_permalink(get_page($r->id));
printf('%1$s<br>', htmlspecialchars($url, ENT_QUOTES, 'UTF-8'));
The %1$s is a positional format specifier; this is done so that the encoded $url value only has to be passed once.
Just concat the string like this:
echo "<a href='". get_permalink(get_page($r->id)) . "'>" . get_permalink(get_page($r->id)) . "</a><br>";
Also if you want to know what's the difference between single and double quotes see this:
What is the difference between single-quoted and double-quoted strings in PHP?
try this way:
if($result as $r)
{
echo "<a href='" . get_permalink(get_page($r->id)) . "'>" . get_permalink(get_page($r->id)) . '</a><br>';
}
I have a problem with my page. I am displaying images using PHP Loop statement. Now I want to assign these images with different id's. Example first loop the first image displayed will have an id="img1", next loop and second image has id="img2". Numbers on the id changes based on the loop iteration variable, while "img" is constant. Here's my code:
for ($i=1;$i<=6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img'.$i>");
}
but it's not working. Any help would be much appreciated.
UPDATE: I got it to work now, thanks for the answers. The working code is:
for ($i=1;$i<=6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img$i'>");
}
You can't use unescaped single quotes if you start your string by single quotes.
These are the possibilities you have:
Using double quotes inside single quotes:
for ($i=0;$i<6;$i++){
echo ('<img src="gangjeong.png" width="113" id="img' . ($i+1) . '" />');
}
Using escaped single quotes inside single quotes (ugly):
for ($i=0;$i<6;$i++){
echo ('<img src=\'gangjeong.png\' width=\'113\' id=\'img' . ($i+1) . '\' />');
}
Using double quotes for starting/ending the string:
for ($i=0;$i<6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img" . ($i+1) . "' />");
}
Using escaped double quotes inside double quotes (ugly):
for ($i=0;$i<6;$i++){
echo ("<img src=\"gangjeong.png\" width=\"113\" id=\"img" . ($i+1) . "\" />");
}
Quotes inside quotes are a no no without proper escaping. Also you have a missing > for the img tag. This should do:
for ($i=1;$i<=6;$i++){
echo "<img src='gangjeong.png' width='113' id='img".$i."'>";
}
Where's your escape characters? Your quotes need escaping, or put your HTML part in double quotes and PHP strings in single, or vice versa. Just make sure you're not confusing start and end quotes.
your syntax is not correct, use:
for ($i=1;$i<=6;$i++){
echo '<img src="gangjeong.png" width="113" id="img'.$i.'" alt="">';
}
Use:
for ($i=1;$i<=6;$i++){
echo "<img src=\"gangjeong.png\" width=\"113\" id=\"img$i\">";
}
I am using like
$myPage .= '<td><a href=\'javascript:editProduct('
.$row['id']
.',"'
.$row['name']
.'")\'>Edit</a></td>';
where $row['name'] has quotes in its value. it breaks. how do i solve the issue both from php side and js side...
$row['name'] is value from DB. and it will have value like pradeep's and pradeep"s also
i used like
$myPage .= '<td><a href=\'javascript:editProduct('.addslashes($row['id']).',"'.addslashes($row['name']).'")\'>Edit</a></td>';
it solves the issue of double quotes. but when i have single quotes in value the javascrit link looks like
javascript:editProduct(28,"pradeep\
it actually breaks..
And how do i strip down the slashes added by addslashes in javascript..
UPDATE - FINAL CODE
$myPage .= '<td><a href=\'javascript:editProduct('.$row['id'].',"'.htmlentities($row['name'],ENT_QUOTES).'")\'>Edit</a></td>';
and js looks like
function editProduct(id,name){
alert(name);
}
can any one solve my issues
Try:
$myPage .= "<td><a href='javascript:editProduct({$row['id']},\""
. htmlentities( $row['name'] )
. "\")'>Edit</a></td>";
htmlentities default behaviour is to convert double quotes and leave single quotes alone, if you require converting single and double quotes, then call it like this:
htmlentities( $row[ 'name' ], ENT_QUOTES )
Also, using { .. } in "..." strings is the correct way to substitute variables.
The PHP string
'<a href=\'javascript:editProduct('.$row['id'].',"'.$row['name'].'")\'>';
outputs (assuming some values)
<td><a href='javascript:editProduct(123,"abc")'></td>
Presumably it breaks if $row['name'] contains a " quote. You could replace such quotes with a \" in the string before you output it using str_replace('"', '\"', $row['name'])