Learn PHP Programming

A backend server scripting language, and a powerful tool for making dynamic and interactive Web pages. Learn each topic with examples and output.

PHP Introduction
Control Structures
Php Forms
Php Advanced

Php Variables

Defining variable

A variable stores a value of any type, e.g., a string, a number, an array, or an object.

A variable has a name and is associated with a value. To define a variable, you use the following syntax:

    $var = 12  // integer variable

    $text = "Hello there" // string variable

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for PHP variables:

Output / Print variables:

The PHP echo statement is often used to output data to the screen.

The following example will show how to output text and a variable:

    <?php
        $txt = "Php";

        echo "I love $txt!";
    ?>

PHP is a Loosely Typed Language

In the example above, notice that we did not have to tell PHP which data type the variable is.

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch

Now that you have done variables in Php, let's learn Echo/Print in php


Share this page on :

=