Forum Moderators: coopster
Is there a better of way instead of this way?
Basically, i want to echo a certain phrase on some pages, but not others. Majority of pages wont need this phrase. At the moment, i'm using an isset statement, and setting a variable at the top of the page like $thisVariable = true;
if (isset($thisVariable)) {
echo "phrase 1";
}
Is there a simpler or better way of doing this? Perhaps reading the file name, and if its a certain file name i can add it to a list or something..
if filename = "file1" or "file2" or "file3"
echo "phrase 1"
Would that be a better way? Or is there a way i dont know of yet?
Does any of that make sense? :)
isset()in your example, and almost certainly gives the wrong result. As an example, consider:
$thisVariable = true;and:
$thisVariable = false;and:
$thisVariable = null;
if( isset( $thisVariable )) {
echo 'TRUE';
} else {
echo 'FALSE';
} Far better is to always declare ALL variables at the top of your script, and use
isset()to provide default values. That way, you cannot receive nasty surprises later on. As an example:
//top of scriptor:
$thisVariable = $value_from_external_source;
if( empty( $thisVariable )) {
$thisVariable = FALSE;
}
//top of script
$thisVariable = isset( $value_from_external_source )
? TRUE
: FALSE;
isset()&
empty()is either can be used and, even with the most stringent error-reporting, will never throw an error. The same cannot be said for other tests, which will usually throw a (welcome)
Noticeif the variable has not been pre-set.