PHP Tutorial
- Introduction to PHP
- Install PHP
- PHP Code
- PHP Echo
- PHP Print
- PHP Echo vs Print
- PHP Variable
- PHP Variable Scope
- PHP $ and $$
- PHP Constants
- PHP Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- Control Statement
Control Statement
- PHP If else
- PHP Switch
- PHP For Loop
- PHP foreach loop
- PHP While Loop
- PHP Do While Loop
- PHP Break
- PHP Continue
PHP Programs
- Pattern Programs
- Numbers Programs
- String Programs
- Array Programs
PHP Functions
- PHP Functions
- Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Arguments
- PHP Variable Arguments
- PHP Recursive Function
PHP Arrays
- PHP Array
- PHP Indexed Array
- PHP Associative Array
- Multidimensional Array
- PHP Array Functions
PHP Strings
- PHP String
- PHP String Functions
PHP Math
- PHP Math Functions
PHP Form
- PHP Form: Get Post
PHP Include
- PHP include & require
State Management
- PHP Cookie
- PHP Session
PHP File
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append File
- PHP Delete File
PHP Variables
In PHP, both echo and print are used to output strings, but there are some differences between them. Here is a comparison of the two:
Declaring Variables
- Variables in PHP are declared with a dollar sign (
$) followed by the variable name. - Variable names must start with a letter or an underscore (
_), followed by any number of letters, numbers, or underscores. - Variable names are case-sensitive.
Syntax
$variableName = value;
Example
Variable Types
PHP supports various data types for variables, including:
- String: Sequence of characters.
- Integer: Whole numbers.
- Float (or double): Floating point numbers.
- Boolean: True or false.
- Array: Collection of values.
- Object: Instance of a class.
- NULL: Variable with no value.
Examples of Different Types
Variable Scope
Variables can have different scopes:
- Local Scope: Variables declared within a function.
- Global Scope: Variables declared outside any function.
- Static Scope: Variables that retain their value across function calls.
Example of Scope
Superglobals
PHP has several predefined variables known as superglobals, which are accessible from any scope within a script. Some examples include:
$_GET$_POST$_SESSION$_COOKIE$_SERVER$_FILES$_REQUEST$_ENV
Example of Superglobals