PHP Tutorial
What is PHP Environment Setup of PHP PHP Syntax PHP Comments PHP Echo & print PHP Variables PHP Scope Variables PHP $ and $$ Variable PHP Constants PHP Operators PHP Data TypesPHP Conditional Statements
PHP Loop Type
PHP Arrays
PHP String PHP FunctionsPHP Form Examples
Form Intro Get & Post Form Validation / Required PHP Complete FormPHP Advanced
PHP Include / Require PHP Date & Time PHP Session PHP Cookies PHP File Handling PHP Open File PHP Read File PHP Write File PHP Append File PHP Delete File PHP File Upload PHP Sending Mail PHP Error HandlingPHP Variables
In PHP, variable main way to store the values or data. Variables are containers that store numeric values, character values, strings, and memory addresses.
There are few rules of PHP Variables:
- A variable always starts with $ symbol, followed by the variable name. Like - $a
- A variable name can't start with a number. It always starts or contain with alphanumeric or underscore (i.e., ‘a-z’, ‘A-Z’, ‘0-9 and ‘_’) in their name.
- PHP variables are case-sensitive, i.e., $var and $VAR. Both are different variables and hold different values.
- A variable name can't contain spaces.
In PHP variable can be declared as: $var_name = Pass Value
Some Examples of PHP Variables:
1) How to declare variables.
<?php
//declaring variables
$text = "This is my First variable"; // string variable
$numeric = 10; //integer variable
//Outputs of Variables
echo $text; //Output : This is my First variable
echo $numeric: //Output : 10
?>
2) Show output of text with variable.
<?php
$text = "Elevenstech Web Tutorials";
echo "Welcome to $text";
//Output : Welcome to Elevenstech Web Tutorials
?>
3) How to concatenate text with variables.
<?php
$text = "Elevenstech";
echo "Welcome to ".$text." Web Tutorials" ;
//Output : Welcome to Elevenstech Web Tutorials
?>
Note: The PHP concatenation operator (.) is used to combine two string values to create one string.
4) How to sum two variables in PHP.
<?php
$number1 = 10;
$number2 = 15;
//result variable hold the sum of two numbers.
$result = $number1 + $number2;
echo "The result is ".$result;
//Output : The result is 25
?>
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date