PHP how set default value to a variable in the class?

You can use a constructor to set the initial values (or pretty much do anything for that matter) as you need to like this:

class example
{

    public $name;

    public function __construct()
    {
        $this->name="first";
    }

}

Then you can use these default values in your other functions.

class example
{

    public $name;

    public function __construct()
    {
        $this->name="first";
    }

    public function test1($inputName)
    {
        if(!empty($inputName))
        {
            $this->name=$inputName;
        }
        echo "The name is ".$this->name."\r\n";
    }

}

$ex=new example();
$ex->test1(" "); // prints first.
$ex->test1("Bobby"); // prints Bobby
$ex->test1($_POST["name"]); // works as you expected it to.

you have two options to set the default values for the class attributes:

Option 1: set at the parameter level.

class A 
{
    public $name = "first";

    public function test1()
    {
        echo $this->name;
    }
}

$f = new A();
$f->test1();

Option 2: the magic method __construct() always being executed each time that you create a new instance.

class A 
{
    public $name;

    public function __construct() 
    {
        $this->name = 'first';
    }

    public function test1()
    {
        echo $this->name;
    }
}

$f = new A();
$f->test1();