6
www.vineetsaini.wordpress.com Variables in PHP Introduction A variable is a thing which is used to store the value or information. The value or information can be text, number, string, and array. In PHP variables are assigned with $ sign. It used before assigning variables in PHP. If you do not use a $ sign before assigning the variables then they will not work. All variables in PHP are prefixed with a dollar sign. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP variables. The syntax of declaring the variable in PHP following is: $variable=value; e.g.:- $name="Vineet"; $num= 40; Rules for Declaring Variables PHP variables must start with (dollar) $ sign. PHP variable name must start with a letter or an underscore "_". e.g.:- $variable or $_variable. PHP variable name can only contain alpha-numeric characters and underscores. PHP variable name should not contain spaces. PHP variables more than one word can also be distinguished with capitalization. e.g.:-$variable Name. PHP variables more than one word should be separated with underscore. e.g.:- $variable_name. Types of variables in PHP PHP variables can have different types which are as follows: Booleans :- That takes two values TRUE or FALSE. integers:- Integers are {...,-2,-1,0,1,2,...}. floating point numbers:- It is also called "doubles" or "real numbers". which represent numbers with decimals. strings:- Strings are set of characters. arrays:- Array is also a variable but array can store more than one value or information. objects:- Objects are user-defined. resources:- Resource variables are created by special PHP functions. PHP is Loosely Type Language PHP is loosely typed language because there is no need for type casting; i.e. there is no need to declare (define) the type and name of the variable before using it. PHP automatically converts the variable to the correct data type, depending on its value. While in a strongly typed programming language you need type casting i.e. you have to declare (define) the type and name of the variable before using it.

Variables in PHP

Embed Size (px)

Citation preview

Page 1: Variables in PHP

www.vineetsaini.wordpress.com

Variables in PHP

Introduction

A variable is a thing which is used to store the value or information. The value or

information can be text, number, string, and array. In PHP variables are assigned with $

sign. It used before assigning variables in PHP. If you do not use a $ sign before assigning

the variables then they will not work. All variables in PHP are prefixed with a dollar sign. The

dollar sign is not technically part of the variable name, but it is required as the first character for the PHP variables. The syntax of declaring the variable in PHP following is:

$variable=value;

e.g.:- $name="Vineet"; $num= 40;

Rules for Declaring Variables

PHP variables must start with (dollar) $ sign.

PHP variable name must start with a letter or an underscore "_". e.g.:- $variable or

$_variable.

PHP variable name can only contain alpha-numeric characters and underscores.

PHP variable name should not contain spaces.

PHP variables more than one word can also be distinguished with capitalization.

e.g.:-$variable Name.

PHP variables more than one word should be separated with underscore. e.g.:-$variable_name.

Types of variables in PHP

PHP variables can have different types which are as follows:

Booleans :- That takes two values TRUE or FALSE.

integers:- Integers are {...,-2,-1,0,1,2,...}.

floating point numbers:- It is also called "doubles" or "real numbers". which

represent numbers with decimals.

strings:- Strings are set of characters.

arrays:- Array is also a variable but array can store more than one value or

information.

objects:- Objects are user-defined. resources:- Resource variables are created by special PHP functions.

PHP is Loosely Type Language

PHP is loosely typed language because there is no need for type casting; i.e. there is no

need to declare (define) the type and name of the variable before using it. PHP

automatically converts the variable to the correct data type, depending on its value. While

in a strongly typed programming language you need type casting i.e. you have to declare (define) the type and name of the variable before using it.

Page 2: Variables in PHP

www.vineetsaini.wordpress.com

Some examples of variables in PHP

1. In the following example we declare a variable with the name var and the value of var is 10.

<?php

$var=10;

echo $var; ?>

Output

2. If you want to add two values then you can add them. In the following example you will

see how to add two values in PHP.

<?php

$var1=10;

$var2=20;

$sum=$var1+$var2;

echo "The sum of 10 and 20 is : ",$sum;

?>

Output

Page 3: Variables in PHP

www.vineetsaini.wordpress.com

3. In the following example we declare a name variable and the value assigned to the

name variable is "Vineet Saini". This is a string example.

<?php

$name="Vineet Saini";

echo $name; ?>

Output

4. If you want to add two strings i.e. first name and last name then you will use the dot (.)

operator. The Dot (.) operator is used to add two strings i.e. concatenate two strings.

Which is shown in the following image.

<?php

$firstname="Vineet";

$lastname="Saini";

echo "The full Name is : " .$firstname." ".$lastname; ?>

Page 4: Variables in PHP

www.vineetsaini.wordpress.com

Output

5. If you want to find the length of your name or any string then you will use the strlen() function. For the new line you will use <br> . Such as in the following image.

<?php

$firstname="Vineet";

$lastname="Saini";

$fullname=$firstname." ".$lastname;

echo "The full Name is : " .$fullname.'<br>';

echo "The length of fullname is : ";

echo strlen($fullname);

?>

Output

6. If you want to find the position of any text/character in a string then you will use the

strpos() function. In the following example we find the position of "Saini" text in the

Page 5: Variables in PHP

www.vineetsaini.wordpress.com

fullname string. Such as in the following image.

<?php

$firstname="Vineet";

$lastname="Saini";

$fullname=$firstname." ".$lastname;

echo "The full Name is : " .$fullname.'<br>';

echo "The position of your text/character is : ";

echo strpos($fullname,"Saini"); ?>

Output

7. In PHP the syntax $name and $$name can be very confusing. But I tell you $name is a

variable and $$name is a variable of a variable. A variable of a variable allows us to change

the name of a variable dynamically.

<?php

$name="Vineet";

$Vineet="Saini";

echo $name.'<br>';

echo $$name; ?>

Output

Page 6: Variables in PHP

www.vineetsaini.wordpress.com

Conclusion

So in this article you saw, how to use of variables in PHP. Using this article you can easily understand variables in PHP. This article is very useful for PHP beginners.