Apparent PHP Syntax Error - php

Basically, I've been trying to make a simple Wordpress widget that displays a QR code with the URL of the current page. I'm using a modififed version of the simple text widget that parses PHP too.
function the_qrcode($permalink = '', $title = '') {
if($permalink && $title == '') {
$permalink = 'http://eternityofgamers.com/forums';
$title = 'Forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>;
}
Can someone tell me what's wrong with this? I get a 500 error when I add it to functions.php.

Look at StackOverflow's syntax highlighting. You're missing a closing single quote ' on your string at the end of the function's last line:
echo '<img ...' .$title. '"/>;
^

Close the last single quote on the echo line to be:
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>';

Related

How To Correct This Code Php Syntax Error

This line of code is perfect for placing the image before the post's title
$title = ‘<img class=”icon_title” src=”‘. $img_source .'” />’ . $title;
But we want to place the image after the post's title. I think my code below is right
$title = $title . ‘ <img class=”icon_title” src=”‘. $img_source .'” />’;
But I am getting this error as shown in the screen shot:
Please can anyone help me to find the solution
It's happending due to your quote problem and you have not wrapped class attribute inside a string. Please follow the below code. It will help you. Convert to your keyboard to standard mode(US) and then type.
$title = $title.'<img class="icon-title" src="'.$img_source.'">';
Your " and ' were not correct type - perhaps you were editing in a word document/application. Consider writing code in applications designed for code to prevent this?
Below is the same line of code with the correct type of ' and ".
$title = $title . '<img class="icon_title" src="'. $img_source .'"/>';

echo background image with a value

I echo out an image like that:
$newString = $thumbPre.'profilemain'.$thumbPost;
echo "<img src='http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error'>";
Now i want the image as a background-image, i tried it like that, but it doesn´t work:
echo '<div style="background-image:url('http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error');"></div>';
you need to use backslashes for nested apostrophes
echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\' alt=\'error\');></div>';
Firstly, remove alt='error' because background-image does not have an alt parameter, img does (you probably thought you could use that from your original code). In trying to use that, your background will not show up.
And your background won't show unless you have content inside that div. I've added Content as an example.
echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\');">Content</div>';
You either have to escape the encapsulating quotes, or remove them altogether.
echo '<div style="background-image:url(http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.');">Content</div>';
Error reporting would have also thrown you a parse error such as:
Parse error: syntax error, unexpected '' alt='' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
http://php.net/manual/en/function.error-reporting.php
This is going to look super confusing but you need to 1, escape the quotes and 2, concatenate your path within that,
let me give you an example. I misread a bit of the question but this will serve you well moving forward especially for cleanliness sake
here is an example:
$imagePath ='PATH TO IMAGE HERE';
echo '<div style="background-image:url(\'' .$imagePath. '\')" >STUFF HERE </div>';

How to correctly write <img src> in php without escaping to HTML

I am having trouble with my PHP code. I've been changing everything for 6 hours and I still get Parse errors no matter what I do. This is the code:
$slider3 = '<img src="'templates/' . $this->template . '/images/slider/slider3.jpg'">' . '" alt="' . $sitename . '" />';
The only way I can figure to not get it to throw an error is by writing it this way:
$slider3 = '<img src="templates/" . $this->template . "/images/slider/slider3.jpg" . "/>"';
but I don't think that's right.
I want $slider3 = "templates/MYTEMPLATE/images/slider/slider3.jpg" then later I will echo $slider3;
I get so confused with all the single and double quotation marks. I think the first one is right - I look at it and study it and it looks right to me. But it throws a parse error.
$slider3 = '<img src="templates/'.$this->template.'/images/slider/slider3.jpg"/>';
should work.
Explanation:
'<img src="templates/'
is a single-quoted string, which happens to contain a double-quote (which is needed for the html src attribute, or any other html attribute value really)
.
(dot) is the string concatenation operator. It concatenates ("glues") the first string together with...
$this->template
which is presumably a string containing the name of the template (not clear from your code example). Note that if $this->template comes from user input, or an otherwise unvalidated source, it could be used for cross-site scripting, eg. if it contains "><script>alert("XSS!")<script>, javascript is executed in the browser!
.
another concatenation with...
'/images/slider/slider3.jpg"/>'
which is another single-quoted string which happens to contain a double-quote, ending the src attribute value.
Try this:
$slider3 = '<img src="templates/"' . $this->template . '"/images/slider/slider3.jpg"/>';
$template = "MYTEMPLATE";
$slider3 = '<img src="templates/'.$template.'/images/slider/slider3.jpg"/>';
echo $slider3;
Will echo - >
<img src="templates/MYTEMPLATE/images/slider/slider3.jpg"/>
Just write:
<?php
$templates = "var";
echo "<img src='templates/${templates}/images/slider/slider3.jpg'/>";
it will result in
<img src='templates/var/images/slider/slider3.jpg'/>

Insert function name with php

I'm trying to do a simple task, insert a second function name under "onmouseover" but probably something is wrong with the syntax.
echo '<div onmouseover="changeText(); <?php if($title==""){echo changeViz();}; ?> ">';
I probably need to escape some quotes and add some, but i can't figure out the correct solution.
Thanks
Nothing it's working. Let me give you the complete code...:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); <?php if($title[$i]==""){echo changeViz();}; ?>">';
You are nesting <?php ?> inside existing php code, which is a syntax error. Instead, concatenate in the javascript function changeViz() as a quoted string.
This version uses a ternary operator to duplicate the if() statement you had originally.
echo '<div onmouseover="changeText(); ' . ($title == '' ? 'changeViz();' : '') . '">';
The ternary operation here will concatenate changeViz(); onto the echo string if $title == "", or otherwise just concatenate on an empty string.
Update after seeing full code:
You have the quote escaping correct in the first part.
echo '<div class="frame" onmouseover="changeText(\'' . $text[$i] . '\'); ' . ($title == '' ? 'changeViz();' : '') . '">';
You can make your code much more readable if you do not try to do everything in one line:
$onmouseover_action = "changeText(); ";
if($title==""){
$onmouseover_action .= "changeViz(); ";
}
echo '<div onmouseover="'.$onmouseover_action.'">';
It makes your code easier to maintain, and you have less need to comment it, because it describes itself quite much better.
Try this:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); '. ($title[$i]=="")?'changeViz();':'').'">';

echo an <img> tag

I'm going crazy trying to echo an <img> tag.
I want to add this simple string "/uploads/" before $photo->name.
Here is my echo: echo '<img src="'.$photo->name.'"/>';
Thanks for any help.
Not sure if I completely understand, but try this:
echo '<img src="/uploads/'.$photo->name.'"/>';
Here you go:
echo '<img src="/uploads/'.$photo->name.'"/>';
Or:
echo '<img src="' . '/uploads/' . $photo->name.'"/>';
Or:
echo '<img src="' . "/uploads/" . $photo->name.'"/>';
echo '<img src="/uploads/'.$photo->name.'"/>';
How about
echo '<img src="/uploads/'.$photo->name.'"/>';
... unless I'm missing something?
two ways to use echo tag
1st : one is double quotes which use for out put of strings
echo "your name is".$name //here name is variable
2nd : one is single quotes in which you can use all html tags here is the example
<?php
$name="Adil";
echo $name;
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ' <table border=3> <tr><td><h1>its image </h1><img src="examp.png" alt="loading" /><td><td>hello</td><tr></table>';
}
}
?>
here i am using multiple tags in echo using
single quotes
if put double quotes then you will see following error
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\Untitled2d.php on line 21
here is the output

Categories