This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Objects in PHP Forum

View Course » View Exercise

922 points
52983e67abf821c6b5005d7f_23743677
Submitted by
Jane Cui
almost 4 years ago

[resolved] 4/10 Why does isAlive print "1" instead of true?

Property Panic (1)

This code works, but why does it pop up "1" and not "true on the screen?

   <?php

    class Person{

        public $isAlive = true;
        public $firstname = "Jane";
        public $lastname = "Cui";
        public $age = 24;

    };

    $teacher = new Person();
    $student = new Person();

    echo $teacher->isAlive;

    ?>

5 votes

permalink

Hi Jane, I have wondered this myself but never bothered to track down the answer - until now - so thanks for asking.

In the php docs there is a section about strings and it says this: "A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values."

Digging a bit deeper I found that you can use var_dump (manual page) to print the type and value of a variable. So:

echo var_dump($teacher->isAlive);

will give you: bool(true)

3698 points
55df2afa95e3787b9200005e_359368177
Submitted by
Judy
almost 4 years ago

2 Comments

52983e67abf821c6b5005d7f_23743677 Jane Cui almost 4 years ago

ok thanks.

55df2afa95e3787b9200005e_359368177 Judy almost 4 years ago

You're welcome.