i
PHP Variables
PHP Data Types
PHP Echo & Print
PHP Strings
PHP Numbers
PHP Constants
PHP Operators
PHP if...else...elseif Statements
Switch Statement
PHP Loops
PHP Arrays
Superglobals
PHP Coding Standards
PHP Form Handling
PHP Form Validation
PHP URLs Validation
PHP Form Required Validation
Complete Form Example
PHP File Functions Open/Read
PHP File Create/Write
PHP File Upload
PHP Cookies Handling
PHP Session Handling
PHP filter_var() Function
PHP Validation Filters
PHP Sanitization Filters
Using Filters
Filters Advanced
JSON
PHP Date and Time
MySQL Database
MySQL Connect
MySQL Commands-Creating a Table
MySQL Commands-Inserting The data
MySQL Commands-Prepared Statement
MySQL Commands-Selecting The Data
MySQL Commands-Where and Order By
MySQL Commands-Deleting And Updating The Data
PHP-OOP Introduction
PHP-Classes/Objects
PHP-Constructor/Destructor
PHP-Access Modifiers
PHP-Inheritance
PHP-Inheritance and Protected Access Modifier
PHP-Overriding Inherited Methods
PHP-Final keyword
PHP-Abstract Classes
PHP-Constants
PHP-Traits
PHP-Static Methods and Properties
Introduction to Functions
Defining A function
Returning Values From A Function
Dynamic Function Calls
Variable Scope
Understanding Arguments Or Parameters
Testing For A Function Existence
Returning Multiple Values From A Function
Making practical Use By Building Code Libraries For Code Re-usability
Using Include() And Require()
Variables
A lot of times, we deal with all source of data and information in Php program and to maintain and keep track of all that data is very difficult. We use variables to hold that data in Php. Imagine that you plan to write a story and you have setup name of a character in your story. You thought to change the name of your character, but you have used the name in your story about 200 times. It will take you so much time and efforts to change the attribute everywhere in your story manually. But with the php variables, you can store that name of the character in a variable and just change the value of the variable to change the character name everywhere without any mistake and with minimal efforts.
Storing the Values
A certain piece of values or data that we want to keep track of, we save that information in variables to organize the data. Variable acts as a container for the piece of information. Syntax of the variable is $ name of the variable and to give the values to your variable add = and value.
For e.g.
$CharacterName = “Xoxo”;
Echo ” Once there was a man named $CharacterName”;
?>
The above script will print the output as Once there was a man named Xoxo. And when you change the variable name at one place, it will reflect the changes wherever it is used.
Modifying the Values
You can also modify the value of your variable throughout the program. You can change the variable value any number of times, and from the place, you want to change. Before modification, the value will remain as before, and after modification, the new value will be reflected. Imagine you have written about one character, and now his story ended, and you want to change the character name, you can modify the character variable value.
For e.g.
$CharacterName = “Xoxo”;
Echo ” Once there was a man named $CharacterName.”;
Echo “$CharacterName died at the age of 80.”;
$CharacterName = “Saba”;
Echo “Then, his daughter named $CharacterName became the princess.”;
?>
It will print the below lines on the webpage.
Once there was a man named Xoxo.
Xoxo died at the age of 80.
Then, his daughter named Saba became the princess.
So, the first part of the story will use the variable name as Xoxo and next part of the story will use the name, Saba. You can update the variable names throughout the php program and change the values. You can store text as well as numbers and other different types of data inside the variables. Let us discuss the datatypes in the next section.
Facts about PHP variables
Php variables can have a short name like x or a and can also have a descriptive name like color or ageofthecharacter. There are some rules of php variables as below.
A variable name starts with a dollar's ($) sign.
A variable name cannot start with a number.
A variable name should only start with a letter or underscore character.
A variable name can have characters, numbers and underscore only.
Variable names are case sensitive.
Php variables take the most recent assignment as their value which is assigned with the = operator. Declaration of the variable is optional. Php converts types from one to another automatically and automatically understands the datatype without specifying it.
Scope of Variables
You can declare a variable anywhere in the php script but has three different scopes, i.e. local, global and static.
Local scope – When we use functions, a variable declared within a function will have a local scope and cannot be used outside a function.
Global scope – A variable that is declared outside a function cannot be used inside a function and can only be used outside a function. If you want to use a variable from a function, add the keyword global before the variable declaration.
Static Scope – Variables will be deleted when a function is called, but if you want a local variable to retain its value after the function is executed using a static keyword before the variable declaration.
Don't miss out!