PHP concatenate html php code [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What am I doing wrong¿?
echo "<img src= 'vestido'.$id.'.jpg' height=500 width=500 >";
I dont know how to concatenate this correctly.
Thank you very much

Basic PHP: If you start a string with one type of quotes e.g. ", you have to END the string with the same type of quotes. PHP is not recursively executable, nor will PHP look inside strings for PHP operators, like concatenation .'s.
You have:
echo "<img src= 'vestido'.$id.'.jpg' height=500 width=500 >";
^---start of string ^--end of string
when it should be
echo "<img src= 'vestido" . $id . ".jpg' height=500 width=500 >";
^--start ^--stop ^---start ^--stop

Your mixing " and '
With " for simple variables you do not need to concat
For ' you have done it correct either replace the start and end " with ' or remove the concat

You are opening your string with a double quote and attempting to close (in order to add a variable) with a single quote. This is not required as you can put variables directly into double quoted strings;
echo "<img src= 'vestido{$id}.jpg' height='500' width='500' >";
In this case I've added curly brackets around the variable to make this obvious. You might need to do this if your variable is immediately followed by a character that is valid in a variable (such as a number or letter) rather than one that isn't (such as a space, hyphen, or dot).
Hope this helps.

You can just insert $id within the echo
<?php
$id=5;
echo "<img src= 'vestido$id.jpg' height=500 width=500 >";
?>
will yield <img src= 'vestido5.jpg' height=500 width=500 >
Based on your code it looked like you wanted to do echo "<img src= 'vestido" . $id . ".jpg' height=500 width=500 >"; which would work as well

Related

syntax error, unexpected 'preg_replace' (T_STRING) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
all I want to know what's the syntax error in this code.
echo "<p id='hide_text_$heading->id' >"preg_replace($rx, "<b>$0</b>", $heading->shortHeadline)"</p>";
Use . to concatenate strings. Change your code to
echo "<p id='hide_text_$heading->id' >" . preg_replace($rx, "<b>$0</b>", $heading->shortHeadline) . "</p>";
to concatenate the three parts or separate each statement:
echo "<p id='hide_text_$heading->id' >";
echo preg_replace($rx, "<b>$0</b>", $heading->shortHeadline);
echo "</p>";
More on String Operators
you forget concatination sympol => .
Also there is syntax error in $0 if you mean by that variable name.
variable name could not start with numbers
only _ and string characters as i remember.
echo "<p id='hide_text_".$heading->id."' >" . preg_replace($rx, "<b>.$0.</b>", $heading->shortHeadline) . "</p>";
You can read more about Concatenation

How to handle special characters like semi colons and quotation marks in php

So I've created a CMS that takes text input. This is how the data is used when I grab it from the database.
echo "<img src=" . $row['image_url'] . " alt=" . $row['caption'] . ">";
Now the problem is, whenever there's a comma or a semi colon, php treats it as part of the code and the page ends up either not rendering well or completely breaking with errors like
Parse error: syntax error, unexpected '=' in
I've tried using htmlspecialcase() when posting the data to the MySQL database but it didn't fix the problem.
EDIT: The main problem is with the alt part not the src part.
When you're using double quotes (") to create a string literal and in that string literal you want to use double quotes ("), then you may escape those double quotes (") to form a valid string.
echo "<img src=\"" . $row['image_url'] . "\" alt=\"" . $row['caption'] . "\">";
You can try like that:
<img src="<?php echo $row['image_url'];?>" alt="<?php echo $row['caption'];?> ">

What is the difference between a . a , and a { in a php echo of variables? [duplicate]

This question already has answers here:
Difference between period and comma when concatenating with echo versus return?
(9 answers)
Closed 7 years ago.
In PHP how do you know if you should use each form of echo, and what is the proper uses for each:
with the period:
echo"<div>
<img src='".$row['image']."'>
".$row['text']."
</div>
with the comma:
echo"<div>
<img src='",$row['image'],"'>
",$row['text'],"
</div>
with the {}:
echo"<div>
<img src='{$row['image']}'>
{$row['text']}
</div>
preset:
$image=$row['image'];
$text=$row['text'];
echo"<div>
<img src='$image'>
$text
</div>
More specifically I'm looking for the difference in how PHP will add the text to the HTML dump on the front end.
. concatenates strings (+ in most languages, but not PHP). PHP manual operators
, separates arguments (echo can take more than 1 argument). PHP manual echo
In your first example
echo" ".$row['text']." must be changed to
echo " ".$row['text']." which is write syntax to concatenate two string.
For second example
echo" ",$row['text']," must be changed to echo " ",$row['text']," I don think this is valid syntax in php .
$image=$row['image']; $text=$row['text']; echo" $text must be like
$image=$row['image']; $text=$row['text']; echo $text;
with period:
echo"<div>
<img src='".$row['image']."'>
".$row['text']."
</div>";
src='".$row['image']."' denotes the concatenation of $row['image']
with comma:
echo"<div>
<img src='",$row['image'],"'>
",$row['text'],"
</div>";
^The comma , defines the string separation in the above example.
preset:
$image=$row['image'];
$text=$row['text'];
echo"<div>
<img src='$image'>
$text
</div>
^While this would still work with single '$image' A better practice would be '".$row['image']."' wrapped around quotes, i.e. below:
echo "<div>
<img src='".$image."'>
".$text."
</div>";
Note: Keeping it simple, . for concatenation and , for separation.
^An Example:
<?php
$str = "Hawas" . "Ka" . "Pujaari";
echo $str;
$str2 = "Hawas, " . "Ka, " . "Pujaari";
echo $str2;
?>

Print variable within html code [duplicate]

This question already has an answer here:
PHP $_Server Not Working Properly [duplicate]
(1 answer)
Closed 8 years ago.
$a="something";
echo ' <div id="content">$a, but not sure how to do it</div>'
How can I print $a's value inside echo?
echo " <div id=\"content\">$a, but not sure how to do it</div>";
Please read the difference between single and double quotes in PHP.
echo "<div id=\"content\">" . $a . ", but not sure how to do it </div>";
The \'s are escape sequences that allow you to have quotation marks within strings (otherwise they would terminate the string). The .'s are concatenation.
there are many ways
1) use " instead of '
echo " <div id='content'>$a, but not sure how to do it</div>"
2) use {} with " if you are want to print something complex and want your editor to format it correctly. most of the time i use it with arrays or when calling a function ex. {$arr['a']} instead of $arr['a'].
echo " <div id='content'>{$a}, but not sure how to do it</div>"
3) just do this
echo ' <div id="content">' . $a . ', but not sure how to do it</div>'

Dynamic ID assigning

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\">";
}

Categories