Page 1 of 1
IF Then Else compare question
Posted: Sun 26 Oct 2014 6:42 pm
by aznetcowboy
I am looking to compare for a blank file, but as I am anything but proficient in PHP, I am not sure how to code a compare looking at a field to see if it is blank and if it isn't display it.
What I am trying to do is:
<?php
if ($wx[field]<>"") // is the $wx[field] blank?
echo $wx[field]; // If not, display it
}
?>
I have been trying to find the correct to do the above, but everything I find on the subject is comparing for numeric value comparisons, not text comparisons. I could use some advice from a more skilled PHP coder than me. which isn't saying a lot for me.

Re: IF Then Else compare question
Posted: Sun 26 Oct 2014 8:26 pm
by BCJKiwi
The best test may depend on what you expect to find in the variable.
There are differences between =, == and ===
This table may be of interest;
isset is_null ===null ==null empty
null | F | T | T | T | T |
unset | F | T | T | T | T |
"" | T | F | F | T | T |
[] | T | F | F | T | T |
0 | T | F | F | T | T |
false | T | F | F | T | T |
true | T | F | F | F | F |
1 | T | F | F | F | F |
\0 | T | F | F | F | F |
Note all the 'unexpected?' T results for isset.
Check these with a query on the internet for php isset and/or php null or similar to get the full explanation.
Re: IF Then Else compare question
Posted: Sun 26 Oct 2014 11:21 pm
by beteljuice
Code: Select all
<?php
echo ($wx[field] ? $wx[field] : "");
?>
Which is the same as:
Code: Select all
<?php
if($wx[field]){
echo $wx[field];
}
Which is the same as:
Code: Select all
<?php
if($wx[field] != ""){
echo $wx[field];
}
ALL of the above are simply looking for $wx[field] to have ANY kind of value. ie. TRUE
EDIT: The first two examples are unsafe ! ... because a value of 0 (zero) would be treated as FALSE.
So for completeness:
Code: Select all
<?php
echo ($wx[field] != "" ? $wx[field] : ""); // you could also say != null
?>
Re: IF Then Else compare question
Posted: Mon 27 Oct 2014 2:07 pm
by SpaceWalker
I would suggest the use of the PHP function 'empty()' rather than 'isset()' as, not only does it check if the variable is false (or empty), it also checks if the variable exists - the function 'empty()' does not generate a warning if the variable does not exist.
So, for your example, you could use:
Code: Select all
if ( empty ( $wx[field] )) {
// display a message indicating the variable is empty
}
else {
echo $wx[field]; // if the variable in not empty
}
Note: The following things/values are considered to be empty:
- "" (an empty string)
•0 (0 as an integer)
•0.0 (0 as a float)
•"0" (0 as a string)
•NULL
•FALSE
•array() (an empty array)
•$var; (a variable declared, but without a value)
Re: IF Then Else compare question
Posted: Mon 27 Oct 2014 11:13 pm
by aznetcowboy
What I am trying to do is make use of the field "currcond" generated by Cumulus' software. It is the field you can enter in some comment like "Today is supposed to be a really nice day!" and it will be displayed on whatever page you put in the pointer to is. For example I am putting it in my index.php page (Saratoga templates).
If I do the following, no problems at all except when I leave the line in the Cumulus page blank, then it displays a blank line.
<font color="darkred">
<?php echo $WX['currcond']?>
</font>
This line then displays between the header and the radar views on my webpage, no problem, except a blank line displays if currcond is blnk.
The currcond in the CUtag.txttmp file shows as follows:
With nothing entered:
currcond|:|:
With "THIS IS A TEST ENTRY - PLEASE IGNORE!" entered:
currcond|Today is supposed to be a really nice day!:|:
What I want to do is have currcond display when it is not blank. So far I have had no luck making it work for me.
Re: IF Then Else compare question
Posted: Tue 28 Oct 2014 12:12 am
by beteljuice
aznetcowboy wrote:.....
If I do the following, no problems at all except when I leave the line in the Cumulus page blank, then it displays a blank line.
<font color="darkred">
<?php echo $WX['currcond']?>
</font>
If you mean you get a line space which you don't want ....
Although it maybe the script above it leaves a paragraph 'space'.
Code: Select all
<?php
if($wx['currcond'] != ""){
echo '<font color="darkred">' .$wx['currcond']. '</font><br />';
}
?>
Re: IF Then Else compare question
Posted: Thu 30 Oct 2014 3:42 am
by aznetcowboy
So far, not good... I am getting nowhere. Below is a chunk of code from my Saratoga index.php:
Code: Select all
<body>
<?php
############################################################################
include("header.php");
############################################################################
include("menubar.php");
############################################################################
//
$useTopWarning = true; // set to true to use only the rss-top-warning script
// set to false to use the rss-advisory script instead
?>
<div id="main-copy">
<?php // insert desired warning box at top of page
if(isset($SITE['NWSalertsCodes']) and count($SITE['NWSalertsCodes']) > 0) {
// Add nws-alerts alert box cache file
include_once("nws-alerts-config.php");
include($cacheFileDir.$aboxFileName);
// Insert nws-alerts alert box
echo $alertBox;
?>
<script type="text/javascript" src="nws-alertmap.js"></script>
<?php
} else { // use atom scripts of choice
if ($useTopWarning) {
include_once("atom-top-warning.php");
} else {
print " <div class=\"advisoryBox\">\n";
$_REQUEST['inc'] = 'y';
$_REQUEST['summary'] = 'Y';
include_once("atom-advisory.php");
print " </div>\n";
}
}
?>
<div class="column-dark">
<div align="center">
<?php echo $WX['currcond']?> <!-- THIS CODE WORKS -->
<?php
echo ($wx['currcond'] ? $wx['currcond'] : ""); // THIS CODE DOES NOT WORK //
?>
<table width="100%" style="border: none">
<tr><td align="center">
<img src="http://icons.wunderground.com/data/640x480/<?php echo $SITE['WUregion']; ?>_rd_anim.gif" alt="Regional Radar" width="320" height="240" style="margin: 0px; padding: 0px; border: none" />
</td>
<td align="center">
<img src="http://icons.wunderground.com/data/640x480/<?php echo $SITE['WUregion']; ?>_ir_anim.gif" alt="Regional Infrared Satellite"
width="320" height="240" style="margin: 0px; padding: 0px; border: none" />
</td>
</tr>
<tr><td colspan="2" align="center"><small>Radar/Satellite images courtesy of <a href="http://www.weatherunderground.com">Weather Underground</a>.</small></td></tr>
</table>
<img src="<?php echo $SITE['imagesDir']; ?>spacer.gif" alt="spacer"
height="2" width="620" style="padding:0; margin:0; border: none" />
<div align="center">
<?php if(isset($SITE['ajaxDashboard']) and file_exists($SITE['ajaxDashboard']))
{ include_once($SITE['ajaxDashboard']);
} else {
print "<p> </p>\n";
print "<p> </p>\n";
print "<p>Note: ajax-dashboard not included since weather station not yet specified.</p>\n";
for ($i=0;$i<5;$i++) { print "<p> </p>\n"; }
}?>
The line where I have
<!-- THIS CODE WORKS --> is what will display something when there is a non-space value in $wx[currcond] or a blank line if $wx[currcond] is blank! Where I have
// THIS CODE DOES NOT WORK // displays nothing, period. I'm hoping this code can give a better picture of what I am talking about.
Re: IF Then Else compare question
Posted: Thu 30 Oct 2014 4:21 am
by PaulMy
<?php echo $WX['currcond']?> <!-- THIS CODE WORKS -->
<?php
echo ($wx['currcond'] ? $wx['currcond'] : ""); // THIS CODE DOES NOT WORK //
Totally unfamiliar with it but would there be any issue with your first line having
$WX in capitals and the second line
$wx in lower case?
Paul
Re: IF Then Else compare question
Posted: Thu 30 Oct 2014 10:43 am
by aznetcowboy
Thanks a bunch! That was the problem. It was right in front of my nose, but too obvious I guess.
Changing
echo ($wx['currcond'] ? $wx['currcond'] : "")
to
echo ($WX['currcond'] ? $WX['currcond'] : "")
made all the difference. Thanks again for seeing this!
