Link to home
Start Free TrialLog in
Avatar of Tony Pearce
Tony PearceFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP embed <script> tag into if else statement

Hi,

I have this code:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "***********";
}
else
{
echo"";
}
?>

end code.


When ever I try to insert this code where the stars are now I get an unexpected < error,


<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>

Any ideas??

Thanks
Avatar of Randy Downs
Randy Downs
Flag of United States of America image

If you are inserting into an existing  block of php code you don't need these tags <?php ?>

In other words something like this

<?php 
some code

<?php 
more code
?>

?>

Open in new window

This is how you would insert the code in the above scenario


<?php
some code


$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "***********";
}
else
{
echo"";
}


?>
First of all, the use of eregi has been deprecated since PHP 5.3 and its continued use is highly discouraged. http://us2.php.net/manual/en/function.eregi.php

I would consider the use of preg_match as a replacement to the use of eregi.

Secondly, your supplied code sample doesn't provide enough detail to effectively help you debug the issue.

~AB
After re-reading this is probably what you need:

http://php.about.com/od/learnphp/qt/php_with_html.htm

<?php
 //your php code here
 ?>

 <b>Here is some more HTML</b>

 <?php
 //more php code
 ?>

 
 </body>
 </html>

Open in new window

Avatar of Tony Pearce

ASKER

Hi,

Sorry I have made myself very unclear..

This is what I'm trying to do and the code as I have tried:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>";
}
else
{
echo"";
}
?>

This code is supposed to show some code if a certain url is used,
Try this:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
?>
<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>

<?php
}
else
{
echo"";
}
?>
Then what you actually want would be this;

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "<script src=\"http://s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js\"></script>";
}
else
{
echo"";
}
?>

Open in new window


Please take note of the escaped double quotes in my code revision. \"

This is required, because without that, PHP will see your string end at the next unescaped double quote.

~AB
Hi,

Thanks, but still getting this error:
Parse error: syntax error, unexpected '<' in **************

any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try:
<?php
$str = $_SERVER['REQUEST_URI'];
if (preg_match('#\broute=checkout/cart#', $str))
{
	echo "<script src='//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js' type='text/javascript'></script>";
}
else
{
	echo "";
}
?>

Open in new window

@hielo: What do you get if you run this script ;-)
echo "";

Open in new window

Just kidding, ~Ray
Well Mr. Paseur, if you can't figure that out a refresher may be necessary.

Allow me to refer you to:
https://www.experts-exchange.com/questions/23681009/need-a-good-tutorial-to-learn-Object-Oriented-Concepts-in-PHP.html?anchorAnswerId=22325869#a22325869

:)