PHP variables are used to store data, such as strings, integers, and arrays. They are denoted by a dollar sign ($) followed by the variable name.
Variables in PHP do not require explicit type declaration. The type is determined by the value assigned to it.
Variable names must start with a letter or underscore, followed by letters, numbers, or underscores.
Variables can be local, global, or static, depending on where they are defined and how they are used within the script.
PHP supports several data types, including strings, integers, floats, booleans, arrays, objects, and NULL.
<?php
$stringVar = "Hello, World!";
$intVar = 42;
$floatVar = 3.14;
$boolVar = true;
echo $stringVar;
?>
In this example, we declare several variables with different data types and output a string variable using the echo statement.
String variables are used to store text. They can be enclosed in either single or double quotes.
Integer variables store whole numbers without decimal points.
Float variables store numbers with decimal points.
Boolean variables can hold only two values: true or false.
Console Output:
Hello, World!
The scope of a variable in PHP defines where the variable can be accessed or modified. PHP has three main scopes: local, global, and static.
Variables declared within a function are local and can only be accessed within that function.
Variables declared outside of any function have global scope and can be accessed anywhere in the script.
Static variables retain their value between function calls and are initialized only once.
<?php
$globalVar = "I am global";
function testScope() {
$localVar = "I am local";
echo $localVar;
}
testScope();
?>
In this example, $localVar
is a local variable and can only be accessed within the testScope
function. $globalVar
is a global variable but is not used in the function.
Local variables are confined to the function they are declared in and are not accessible outside of it.
Global variables can be accessed globally across different functions by using the global
keyword or the $GLOBALS
array.
Static variables are initialized only once and retain their value between function calls.
Console Output:
I am local
PHP allows the use of variable variables, where the name of a variable can be set and used dynamically.
By using two dollar signs ($$), you can create a variable whose name is stored in another variable.
Variable variables are useful when you need to access variables dynamically, such as in loops or when dealing with dynamic data sources.
<?php
$a = "hello";
$$a = "world";
echo $hello;
?>
In this example, $a
contains the string "hello". Using variable variables, $$a
creates a new variable named $hello
with the value "world".
Variable variables allow for dynamic variable names, which can be useful in dynamic programming scenarios.
While variable variables are powerful, they can make code harder to read and debug, so use them judiciously.
Console Output:
world
Superglobals are built-in variables in PHP that are always accessible, regardless of scope. Examples include $_GET
, $_POST
, $_SESSION
, and more.
Superglobals are used to retrieve data from forms, sessions, cookies, and server information.
Superglobals are arrays containing data that can be accessed directly without needing to declare them globally.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
echo "Hello, " . $name;
}
?>
This example uses the $_POST
superglobal to retrieve data from a form submission and outputs a greeting message.
Superglobals like $_POST
and $_GET
are commonly used to handle form data in PHP applications.
Always validate and sanitize user input obtained from superglobals to prevent security vulnerabilities such as SQL injection.
Console Output:
Hello, [Name]
Constants in PHP are similar to variables, except that once they are defined, they cannot be changed or undefined.
Constants are defined using the define()
function and are always global.
By convention, constant identifiers are usually written in uppercase letters.
<?php
define("GREETING", "Welcome to PHP!");
echo GREETING;
?>
In this example, a constant named GREETING
is defined with the value "Welcome to PHP!" and is output using the echo
statement.
Constants are automatically global and can be used across the entire script.
Once a constant is defined, its value cannot be changed. This makes constants ideal for values that should remain constant throughout the script.
Console Output:
Welcome to PHP!
Arrays in PHP are used to store multiple values in a single variable. They can hold a collection of data of various types.
PHP supports indexed arrays (numeric keys), associative arrays (named keys), and multidimensional arrays.
Arrays can be created using the array()
function or by using the short array syntax []
.
<?php
$fruits = array("Apple", "Banana", "Cherry");
echo $fruits[0]; // Outputs: Apple
?>
This example demonstrates the creation of an indexed array containing a list of fruits and outputs the first element.
Indexed arrays use numeric keys to access their elements.
Associative arrays use named keys that you assign to them.
Multidimensional arrays contain one or more arrays, allowing for more complex data structures.
Console Output:
Apple
Associative arrays in PHP are arrays that use named keys that you assign to them, rather than numeric keys.
Associative arrays can be created using the array()
function with key-value pairs or the short array syntax []
.
Elements in an associative array are accessed using their named keys.
<?php
$age = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
echo "Peter is " . $age['Peter'] . " years old.";
?>
In this example, an associative array is created to store ages with names as keys, and it outputs Peter's age.
Associative arrays store data in key-value pairs, allowing for meaningful access to data.
Associative arrays provide flexibility in organizing and accessing data, especially when dealing with structured data sets.
Console Output:
Peter is 35 years old.
Multidimensional arrays are arrays containing one or more arrays. They are useful for storing data in a tabular form.
These arrays can be created using nested arrays within the array()
function or the short array syntax []
.
Elements are accessed using multiple indices, corresponding to the depth of the array.
<?php
$cars = array(
array("Volvo", 22, 18),
array("BMW", 15, 13),
array("Saab", 5, 2),
array("Land Rover", 17, 15)
);
echo $cars[0][0] . ": In stock: " . $cars[0][1] . ", sold: " . $cars[0][2] . ".";
?>
This example creates a multidimensional array to store car models and their stock and sales numbers, outputting data for the first car.
Each element of a multidimensional array can itself be an array, allowing for complex data structures.
Multidimensional arrays are ideal for representing data in a matrix or table format.
Console Output:
Volvo: In stock: 22, sold: 18.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies