PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, a variable starts with the $ sign, followed by the name of the variable.
Here is a brief tutorial:
1. Declaring Variables
To declare a variable in PHP, you use the $ sign followed by the variable name. For example:
$myVariable;
2. Assigning Values to Variables
You can assign a value to a variable using the equal (=) operator. For example:
$myVariable = "Hello, World!";
3. Variable Types
PHP supports several types of variables:
Here is an example of each:
$intVar = 123; // Integer $doubleVar = 12.3; // Double $strVar = "Hello, World!"; // String $boolVar = true; // Boolean $arrayVar = array("apple", "banana", "cherry"); // Array $nullVar = NULL; // Null
4. Displaying Variable Values
You can display the value of a variable using the echo
statement:
echo $myVariable;
5. Variable Scope
In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. There are three types of variable scope:
Here is an example of each:
$globalVar = "I'm a global variable!"; // Global variable function testFunction() { $localVar = "I'm a local variable!"; // Local variable static $staticVar = 0; // Static variable $staticVar++; echo $localVar; echo $staticVar; } testFunction(); echo $globalVar;
Remember, local and global are different scopes. To use a global variable inside a function, you need to use the global
keyword:
$globalVar = "I'm a global variable!"; function testFunction() { global $globalVar; echo $globalVar; } testFunction();
This is a brief introduction to PHP variables. There's a lot more to learn about PHP, but this should give you a good start.
How to declare variables in PHP:
Variables in PHP are declared using the $
sign followed by the variable name.
<?php $variableName = "Hello, World!"; echo $variableName;
PHP variable types and examples:
PHP is loosely typed; variables are dynamically assigned types.
<?php $integerVar = 42; $stringVar = "Hello"; $floatVar = 3.14; $boolVar = true;
Variable naming conventions in PHP:
<?php $myVariable = "Example"; $user_name = "John";
Scope of variables in PHP:
<?php $globalVar = "I'm global"; function myFunction() { $localVar = "I'm local"; echo $localVar; } myFunction(); echo $globalVar;
Global variables in PHP:
<?php $globalVar = "I'm global"; function myFunction() { global $globalVar; echo $globalVar; } myFunction();
Static variables in PHP:
<?php function counter() { static $count = 0; $count++; echo $count; } counter(); // Outputs: 1 counter(); // Outputs: 2
Superglobal variables in PHP:
Superglobals are predefined variables in PHP. Examples include $_GET
, $_POST
, $_SESSION
, etc.
<?php echo $_SERVER['HTTP_USER_AGENT'];
PHP variable interpolation:
<?php $name = "John"; echo "Hello, $name!";
PHP variable concatenation:
<?php $firstName = "John"; $lastName = "Doe"; echo $firstName . " " . $lastName;
Dynamic variables in PHP:
<?php $dynamicVarName = "user"; $$dynamicVarName = "John"; echo $user; // Outputs: John
Variable variables in PHP:
<?php $firstName = "John"; $lastName = "Doe"; $variableName = "firstName"; echo $$variableName; // Outputs: John
Constants vs. variables in PHP:
<?php define("MY_CONSTANT", "Hello, Constant"); $variable = "Hello, Variable"; echo MY_CONSTANT; echo $variable;
PHP variable assignment and initialization:
<?php $x = 5; // Initialization $y = $x + 10; // Assignment based on a computation echo $y;