setting 0 or 1 to show image

pileofcrap

New member
Oct 17, 2006
569
2
0
Atlanta, GA
Ok so I'm writing a new script to release for free. I am not good at php but I have managed to write a functional script that isn't causing errors.

Now... in the config file I have
PHP:
$sitelogo = 'URL To image/logo'; // This is subject to the actual location of your logo / header
$showimg = '0'; // 0 hides the logo, 1 shows the logo
So basically if someone selects 0 i want the $sitelogo to not display and if they select 1 I want it to show up.

Here is my code:
PHP:
<?php
if ($showimg = 0) {
    echo "<h1>$sitename</h1>";
} else {
    echo "<h1>$sitename<br /><img src='$sitelogo'></h1>";
}
?>

For some reason it only displays the Else Echo regardless of if I set it to 0

Feel free to flame but hopefully amongst the spinning dicks someone can help me. I'm sure this is some very basic beginner stuff but I can't find anything on it.
 


you want "==", instead of "=" on the second line of your PHP.
right now, you're **reassigning** $showimg to 0 every time you hit that for loop; use the double-equals operator to check for equivalence, single equals operator is assignment.
 
Try
if ($showimg == 0) {

instead of
if ($showimg = 0) {

= is an assignment opperator == and === are comparison opperators. If somehow $showimg becomes anything else it would still show.
 
Bonus question: would it make a difference to use == vs. === ?

Sometimes. It depends if you are comparing a string value with a function that may return a 0 or 1 as boolean true/false. The === would try to match the string literal whereas a == could compare a value to a null and return 0 (as opposed to '0'), though the rest of your code may interpret it as equal and you spend days trying to debug it.

Always check the API to see if a function returns boolean false, especially when dealing with strings
 
Not in this case, no.
= -> assignment
== -> equivelance
=== -> identical

Code:
---------------
with strings
---------------
$pet = "dog";
($pet = "dog") --> returns "dog" (first, "dog" is assigned to $pet, then returns $pet)
($pet == "dog") --> returns true ($pet is equivelant to "dog")
($pet === "dog") --> returns true ("a" is identical to "a", in PHP --
note that some languages will actually treat this as false, because "dog" is a new string,
and $pet is set to the old string defined in the first line; since they reference the same string
but in different spaces of the memory, they're "not identical")


---------------
with objects
---------------

class Dog {
  $name = "";
  function __constructor() { echo "woof! "; }
}
$pet = new Dog();
// 'woof! '
$pet->name = "bitch please";
// notice that we're naming the dog here


if($pet = new Dog()) echo 'TRUE';
// 'woof! TRUE' --> you just created ANOTHER dog()
echo $pet->name;
// "" empty --> $pet is now set to the new copy of the dog, not the original.


if($pet == new Dog()) echo 'TRUE';
// 'woof! TRUE' --> a new Dog() is "equivelant" to any other new Dog(), since neither of them have had their name changed

$pet->name = "snoop";
if($pet == new Dog()) echo 'TRUE';
// 'woof! ' --> now that the first name is different, new Dog() != $pet

$pet = new Dog();
// 'woof! ' --> setting up a new dog for the next example
if($pet === new Dog()) echo 'TRUE';
// 'woof! ' --> new dog created, but they are not PRECISELY the same dog, so the identity test fails

read this -- http://www.php.net/manual/en/language.operators.comparison.php