SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
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.



                    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




                   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;
?>


                    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



                    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




                    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.




                    www.vineetsaini.wordpress.com

Mais conteúdo relacionado

Mais procurados

03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblockIIUM
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operatorsKhem Puthea
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics McSoftsis
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHPAhmed Swilam
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in functiontumetr1
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPprabhatjon
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular ExpressionsAnand Sharma
 

Mais procurados (17)

03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Variables
VariablesVariables
Variables
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
PHP
PHP PHP
PHP
 
Data Types Master
Data Types MasterData Types Master
Data Types Master
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Dades i operadors
Dades i operadorsDades i operadors
Dades i operadors
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 

Semelhante a Variables in PHP

P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kianphelios
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kzsami2244
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfpkaviya
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptxJapneet9
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)Core Lee
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009cwarren
 
Introduction to php 3
Introduction to php   3Introduction to php   3
Introduction to php 3pctechnology
 
Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomerShivi Tomer
 

Semelhante a Variables in PHP (20)

Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Php essentials
Php essentialsPhp essentials
Php essentials
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Babitha5.php
Babitha5.phpBabitha5.php
Babitha5.php
 
Php
PhpPhp
Php
 
Babitha5.php
Babitha5.phpBabitha5.php
Babitha5.php
 
Babitha5.php
Babitha5.phpBabitha5.php
Babitha5.php
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 
Php
PhpPhp
Php
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009
 
basics of php
 basics of php basics of php
basics of php
 
Introduction to php 3
Introduction to php   3Introduction to php   3
Introduction to php 3
 
Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomer
 

Mais de Vineet Kumar Saini (20)

Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 

Último

Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 

Último (20)

Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

Variables in PHP

  • 1. 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. www.vineetsaini.wordpress.com
  • 2. 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 www.vineetsaini.wordpress.com
  • 3. 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; ?> www.vineetsaini.wordpress.com
  • 4. 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 www.vineetsaini.wordpress.com
  • 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"); ?> 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 www.vineetsaini.wordpress.com
  • 6. 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. www.vineetsaini.wordpress.com