PHP-Paragraph in Echo statement error? - php

I want to echo these two strings. This is my current code:
echo str_replace(array_keys($swears), array_values($swears), $main).<br />;
echo str_replace(array_keys($swears), array_values($swears), $post).<br />;
But it produces this error:
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\new\new.php
Please let me know your feedback.

Use this instead since you have to use quotes for displaying HTML tags:
echo str_replace(array_keys($swears), array_values($swears), $main)."<br />";
echo str_replace(array_keys($swears), array_values($swears), $post)."<br />";

echo str_replace(array_keys($swears), array_values($swears), $main) . '<br />';

Ths is a string, so it has to be quoted.
echo str_replace(array_keys($swears), array_values($swears), $main). '<br />';

Related

RSS Feed parse showing error after :

This is a strange one for me, i am parsing an rss feed using simplexml_load_file, it's working fine up until:
<?php
$xml = simplexml_load_file($url);
$items = $xml->channel->item;
foreach($items as $offer)
{
echo $offer->title;
echo "<br />";
echo $offer->guid;
echo "<br />";
echo $offer->description;
echo "<br />";
echo $offer->campinfo:amount;
echo "<br />";
echo $offer->campinfo:country;
echo "<br />";
echo $offer->campinfo:type;
echo "<br />";
echo "<hr>";
?>
It hits these parts:
$offer->campinfo:amount; the ":" is causing the script to error out, Parse error: syntax error, unexpected ':', expecting ',' or ';'
I cannot find any information on this, any help would be appreciated.
EDIT: example added
<item>
<title>win iPhone 6s!</title>
<link>http://qckclk.com/offer.php?id=341201&pub=240627&subid=</link>
<guid>http://qckclk.com/offer.php?id=341201&pub=240627</guid>
<description>il suffit d';entrer votre numéro pour gagner 6s iPhone!</description>
<campinfo:amount>10.24</campinfo:amount>
<campinfo:campid>341201</campinfo:campid>
<campinfo:country>LU</campinfo:country>
<campinfo:type>Pin+Submit</campinfo:type>
<campinfo:epc>1.01</campinfo:epc>
<campinfo:ratio>9</campinfo:ratio>
</item>
: is not valid in a variable name. If you need to access a property that isn't a valid identifier, you need to use {"string"} notation:
echo $offer->{"campinfo:amount"};
http://php.net/manual/en/language.variables.basics.php
A valid variable name starts with a letter or underscore, followed by
any number of letters, numbers, or underscores
As a regular expression, it would be expressed thus:
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
echo $offer->campinfo:amount;
contains : in the variable, which isnt legal according to the manual.
Change that or var_dump ($offer) to see whats actually in there
For anyone needing to know in future the answer was:
$offer->children('campinfo', true)->amount;

PHP Syntax Check: Parse error: syntax error, unexpected [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
Cant figuer out the problem with my code. Theres a syntax error,and i just cant spot it
Checked it here - http://phpcodechecker.com/ , and got this as an answer -
Parse error: syntax error, unexpected ''.$val[''
(T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in your code on
line 10
Here is the code -
<?php
include "products.php";
foreach($products as $key=>$val)
{
echo '<div style="float:right;text-align:center;margin:20px;">';
echo '<IMG src="img/'.$val['category'].'/'.$val['image'].'" WIDTH="94" HEIGHT="94" BORDER="0" ALT=""><br />';
echo $val['name'].'<br />';
echo '₪ '.$val['price'].'<br />';
echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>';
echo '</div>';
}
?>
Replace
echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>';
to
$imgtag = 'img/'.$val['category'].'/'.$val['image'];
echo '<a onmousedown="parent.AddItemToCart(\''.$val['id'].'\',\''.$val['name'].'\',\''.$imgtag.'\',\''.$val['price'].'\')"></a><br/>';
The unescaped ' terminates the string before you want it to. Try:
echo '<a onmousedown="parent.AddItemToCart(\''.$val['id'].'\' ,\''.$val['name'].'\','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'\')"></a><br/>';
or use double quotes:
echo "<a onmousedown=\"parent.AddItemToCart('".$val['id']."' ,'".$val['name']."','img/".$val['category']."/".$val['image']."','.$val['price']."')\"></a><br/>';
Reason: The reason of this happening, is that you use ' to start and end your string, but your string also contains the ' character. This way the string ends at the position you enter ' and is confused by the following symbols. To prevent this, you escape the symbols using a backslash \ like so: 'some string containing a \' character'
Change:
echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>';
For this:
echo "<a onmousedown='parent.AddItemToCart(".$val['id']. ',' .$val['name']. ',img/'.$val['category'].'/'.$val['image'].','.$val['price'].")></a><br/>";
Change the code with given code
<?php
include "products.php";
foreach($products as $key=>$val)
{
echo '<div style="float:right;text-align:center;margin:20px;">';
echo '<IMG src="img/'.$val['category'].'/'.$val['image'].'" WIDTH="94" HEIGHT="94" BORDER="0" ALT=""><br />';
echo $val['name'].'<br />';
echo '₪ '.$val['price'].'<br />';
echo '<a onmousedown="parent.AddItemToCart('.$val['id'].','.$val['name'].'','img/'.$val['category'].'/'.$val['image'].','.$val['price'].')"></a><br/>';
echo '</div>';
}
?>

How can i echo a variable from my database inside my echo?

Okay so i was wondering how i echo a variable from a database that i already have connected to a php script so basicly it would be like
echo '<img src="blablabla.com/VARIABLENAMEHERE" />'
Use double quotes, and then variables will be expanded inside the string:
echo "<img src='blablabla.com/$variable' />";
Or user string concatenation:
echo '<img src="blablabla.com/' . $variable . '" />';
you can use double quotes as the double quotes evaluates the variable inside.
echo "<img src='blablabla.com/$variablename' />";
but if you insist using single quote you can use concatenation.
echo '<img src="blablabla.com/' . $variablename . '" />';

Why differences in output using SUPERGLOBAL in PHP?

I am getting error by running following code:
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo $var=>$value.'<br />'; //this will result in to following error:
//Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ';' in
//C:\xampp\htdocs\w_j_gilmore\CH_03_PHP_BASICS\superglobal.php on line 6
}
?>
And following code runs successfully
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo "$var=>$value<br />";
}
?>
Printing in single quotation and double quotation is difference.
WHY?
The difference between the 2 is that in the first you are trying to use the => operator (which is not valid in that place, so will result in a syntax error), while in the second you print a string which happens to have the characters = and > in it.
The second effectively could be rewritten as:
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo $var . "=>" . $value . "<br />";
}
?>
If you are just trying to output $_SERVER for debug reasons, I suggest using var_dump or print_r
var_dump($_SERVER);
You have not quoted the string:
echo $var=>$value.'<br />';
quoted would look like this:
echo '$var => $value <br />';
if single quoted, variables are not interpreted.

formatting a php variable into a web address

can you guys help me out with this, it should be easy but after 6 hours of work its frying my brain.
echo '<img src="', $webadd['data']['0']['address'], '"/>;
I just want it to be like:
<img src="http://www.image.com" />
the variable in the middle is the web address.
echo '<img src="'.$webadd['data']['0']['address'].'"/>';
echo "<img src='{$webadd['data']['0']['address']}'/>";
This should work:
echo '<img src="' . $webadd['data']['0']['address'] . '" />';
echo "<img src=\"" . $webadd['data']['0']['address'] . "\" />";
Consider adding spaces around the dots for readability.
You missed . by ,
Try this:
echo '<img src="'. $webadd['data']['0']['address'].'"/>;
or
echo "<img src=".'{$webadd['data']['0']['address']}'."/>";
TC lost to add qoutes to the end. Its works fine http://3v4l.org/5JCNc :)
$webadd['data']['0']['address'] = 'http://www.image.com';
echo '<img src="',$webadd['data']['0']['address'],'"/>';
See Example #1(separate by commas) from http://www.php.net/manual/en/function.echo.php.

Categories