If Statement With nested functions fails

I am new to php but I have read all the stuff on if, elseif, nested functions and found a few errors in my code (and resolved them) but there is one that I just can’t fathom and I hope someone can tell me what I have done wrong.

I have a php IF function that goes out to a third party website and determines if a specified image file exists; if first file is not found then the ELSEIF determines if a second specified image file exists; if the second specified image is not found either then a simple text message is displayed.

If either the first or the second image file exists then I use the getimagesize() function to extract the width and height of the image and compute new width and height parameters so that the image is displayed, correctly proportioned, on my web page, based on my maximum page-element width.

What is going wrong is that once a specified image is found to exist, the function that is activated, in either the IF or ELSEIF statement, fails to execute correctly. Specifically, the activated function contains two nested <?php . . .> statements and the execution fails at the point of reading the first <?php . . .> statement.

[see below] The activated function fails at <?php echo date('d-M-Y');?> in either instance of an image file being found to exist.

Note: the elements within echo “<h3 align=. . .” display correctly if placed on the webpage independently. It is only when I combine them into the IF, ELSEIF environment that they fail.

Code for analysis:

<?php $url1 = 'http://www.usno.navy.mil/NOOC/nmfc-ph/RSS/jtwc/warnings/wp0711.gif'; $url2 = 'http://www.usno.navy.mil/NOOC/nmfc-ph/RSS/jtwc/warnings/wp0611.gif'; if (fopen($url1, "r")) { echo "

Latest Information Typhoon Track 07W <?php echo date('d-M-Y');?>

												<p align=\"center\">
                                                <a href=\"http://www.usno.navy.mil/NOOC/nmfc-ph/RSS/jtwc/warnings/wp0711.gif\">
												<?php

list($width, $height, $type, $attr)=getimagesize(‘http://www.usno.navy.mil/NOOC/nmfc-ph/RSS/jtwc/warnings/wp0711.gif’);
$ht=$height;
$wd=$width;
$max=480;
if($width > $max){
$diff = $width-$max;
$percnt_reduced = (($diff/$width)100);
$ht = round($height-(($percnt_reduced
$height)/100));
$wd= $width-$diff;
}
echo “<img src=”’.‘http://www.usno.navy.mil/NOOC/nmfc-ph/RSS/jtwc/warnings/wp0711.gif’.’" height="’.$ht.’" width="’.$wd.’" alt=“Latest Position of current typhoon 06W Philippines” title=“Latest Typhoon Track Click to see original at JTWC” border=“3” style=“border-color:#FFFFFF” >"; ?>

" ;

} elseif (fopen($url2, “r”)) {

echo "<h3 align=\"center\">Latest Information Typhoon Track 06W <?php echo date('d-M-Y'); ?></h3>
												<p align=\"center\">
                                                <a href=\"http://www.usno.navy.mil/NOOC/nmfc-ph/RSS/jtwc/warnings/wp0611.gif\">
												<?php

list($width, $height, $type, $attr)=getimagesize(‘http://www.usno.navy.mil/NOOC/nmfc-ph/RSS/jtwc/warnings/wp0611.gif’);
$ht=$height;
$wd=$width;
$max=480;
if ($width > $max) {
$diff = $width-$max;
$percnt_reduced = (($diff/$width)100);
$ht = round($height-(($percnt_reduced
$height)/100));
$wd= $width-$diff;
}
echo “<img src=”’.‘http://www.usno.navy.mil/NOOC/nmfc-ph/RSS/jtwc/warnings/wp0611.gif’.’" height="’.$ht.’" width="’.$wd.’" alt=“Latest Position of current typhoon 06W Philippines” title=“Latest Typhoon Track Click to see original at JTWC” border=“3” style=“border-color:#FFFFFF” >"; ?>

";

} else {
echo “<h4 align=“center”>There are no current Typhoon Warnings in effect . . . cool”;
}
;
clearstatcache(); ?>

Hello Martyn,

Redeclaring <?php...?> inside <?php ...?> can sometimes get you in trouble. So, be careful next time.

Here’s my solution for your problem:
[php]

<?php $url1 = 'http://www.usno.navy.mil/NOOC/ nmfc-ph/RSS/jtwc/warnings/wp0711.gif'; $url2 = 'http://www.usno.navy.mil/NOOC/ nmfc-ph/RSS/jtwc/warnings/wp0611.gif'; if (fopen($url1, "r")){ echo '

Latest Information Typhoon Track 07W ' . date('d-M-Y') . "

'; list($width, $height, $type, $attr)=getimagesize('http:// www.usno.navy.mil/NOOC/nmfc-ph/RSS/ jtwc/warnings/wp0711.gif'); $ht=$height; $wd=$width; $max=480; if($width > $max){ $diff = $width-$max; $percnt_reduced = (($diff/$width)*100); $ht= round($height-(($percnt_reduced* $height)/100)); $wd= $width-$diff; } echo 'Latest
Position of current typhoon 06W Philippines

'; } elseif (fopen($url2, "r")) { echo '

Latest Information Typhoon Track 06W ' . date('d-M-Y') . '

'; list($width, $height, $type, $attr)=getimagesize('http:// www.usno.navy.mil/NOOC/nmfc-ph/RSS/ jtwc/warnings/wp0611.gif'); $ht=$height; $wd=$width; $max=480; if ($width > $max) { $diff = $width-$max; $percnt_reduced = (($diff/$width)*100); $ht = round($height-(($percnt_reduced* $height)/100)); $wd= $width-$diff; } echo 'Latest
Position of current typhoon 06W Philippines

'; } else { echo '

There are no current Typhoon Warnings in effect . . . cool

'; } clearstatcache(); ?>

[/php]

Cheers…

Thank you codeguru . . . I can see you solved the problem by replacing the <?php with a period ( . )

You have said that redeclaring the <?php statement is sometimes problematic . . . is there some definitive rule book out there on the Internet that can tell me when I am are about to get into trouble with this? Or is it just best practice to always use the period ( . )

I note you also removed the escaped quotation marks ( " ) from within the echoed content and used single quotes ( ’ ) to encapsulate the entire echoed content. Does this work in all cases i.e. if I encapsulate the echoed content within single quotes ( ’ ) can I dispense completely with the need to escape quotation marks ( " ) within?

Once again, sincere thanks for your deliberation and solution . . . the issuing of Typhoon Warnings in the Philippines will now be more efficient

You only have to dispatch another <?php ?> tags if you’re outside the PHP scripting block. If a block of PHP codes has been closed with ?> that’s the time you encapsulate your PHP statements again with <?php ?>.

As to the backslash (), single/double quotes, I understand why you’ve escaped the doublequote with a backslash ("). This is to make sure that the double quote will not cause any error during parsing. Now, for me there really is no problem with that. My experience taught me that we can always enclose any value to an attribute, say , with double quote within the echo statement so long as your echo statement is encapsulated within single quotes. Example:

[PHP]echo ‘This is a URL’;[/PHP]

Of course, you can also do it this way:

[PHP]echo “This is a URL”;[/PHP]

Hope this helps… :slight_smile:

Thank you for the clarifications . . . I can see I can master this php with a little help :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service