PHP - Introduction Creating Variables

Introduction

Creating a variable is also known as declaring it.

Declaring a variable is to use its name in your script:

$my_first_variable;

When PHP first sees a variable's name in a script, it creates the variable at that point.

When declaring a variable in PHP, it's good practice to assign a value to it at the same time.

This is known as initializing a variable.

If you don't initialize a variable in PHP, it's given the default value of null .)

Here's an example of declaring and initializing a variable:

$my_first_variable = 3;

Here, the code creates the variable called $my_first_variable, and uses the = operator to assign it a value of 3.

The following script creates two variables, initializes them with the values 5 and 6 , then outputs their sum ( 11 ):

Demo

<?php
    $x = 5;//from   w  w  w  .j  a v  a 2s . c  om
    $y = 6;
    echo $x + $y;
?>

Result

Related Topic