Variables in PHP

1,147 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,147
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Variables in PHP

  1. 1. Variables in PHPIntroductionA variable is a thing which is used to store the value or information. The value orinformation 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 assigningthe variables then they will not work. All variables in PHP are prefixed with a dollar sign. Thedollar sign is not technically part of the variable name, but it is required as the firstcharacter 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 PHPPHP 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 LanguagePHP is loosely typed language because there is no need for type casting; i.e. there is noneed to declare (define) the type and name of the variable before using it. PHPautomatically converts the variable to the correct data type, depending on its value. Whilein 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. www.vineetsaini.wordpress.com
  2. 2. Some examples of variables in PHP1. In the following example we declare a variable with the name var and the value of var is10.<?php$var=10;echo $var;?>Output2. If you want to add two values then you can add them. In the following example you willsee 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 www.vineetsaini.wordpress.com
  3. 3. 3. In the following example we declare a name variable and the value assigned to thename variable is "Vineet Saini". This is a string example.<?php$name="Vineet Saini";echo $name;?>Output4. 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;?> www.vineetsaini.wordpress.com
  4. 4. Output5. 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);?>Output6. If you want to find the position of any text/character in a string then you will use thestrpos() function. In the following example we find the position of "Saini" text in the www.vineetsaini.wordpress.com
  5. 5. 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");?>Output7. In PHP the syntax $name and $$name can be very confusing. But I tell you $name is avariable and $$name is a variable of a variable. A variable of a variable allows us to changethe name of a variable dynamically.<?php$name="Vineet";$Vineet="Saini";echo $name.<br>;echo $$name;?>Output www.vineetsaini.wordpress.com
  6. 6. ConclusionSo in this article you saw, how to use of variables in PHP. Using this article you can easilyunderstand variables in PHP. This article is very useful for PHP beginners. www.vineetsaini.wordpress.com

×