Variables in PHP - Core PHP Tutorial Chapter 4

Variables in PHP – Core PHP Tutorial Chapter 4

Variables are used in PHP and other programming languages to store dynamic values.

Variables can be used in any PHP program to store information.

How to create or declare a variable in PHP?

Variables are declared using $ (dolar) sign followed by _ (underscore) or alphabets.

If we want to declare a variable named First Name we can write it as:

$firstName

A variable name can contain Alpha Numeric Characters with or without underscores.

A variable can store values of any data type like strings, bool, float, double or integer.

Remember that in PHP the variables are declared in the same line where a value is assigned to them.

Unlike other programming languages there is no need to firstly declare a variable and then assign a value to it.

You can assign values and declare a variable at the same time.

Here is an example:

<?php

$firstName = “Manish”;

?>

If we want to print this variable we can simply write

<?php

$firstName = “Manish”;

echo $firstName;

//Outputs : Manish

?>

Variables in PHP are case sensitive.

Note : In the example given above we have not used single quotes or double quotes while echoing or printing $firstName. But a string cannot be assigned to a variable without the use of double or single quotes.

Consider the example given below

<?php

$lang = “PHP”;

echo   “We are studying $lang”;

//Outputs : We are studying PHP.

?>

In this example we have written $lang inside double quotes. We will not get any error because the assigned value to $lang variable was a string.

You can watch out Step by Step Tutorial About Variables in PHP

You can join out Php Training in Lucknow

Leave a Comment