Variables are storage of different types of information. When declaring a variable, it cannot start with a number, it starts with the "$" sign, followed by the variable name (e.g. code). It can only contain underscore and alphanumeric characters. ′ code). It can only contain underscore and alphanumeric characters. 'cod e ) . It can only contain underscore and alphanumeric characters .' code' and '$CODE' are two different variables because it is case sensitive.
Local, global, and static are three different variable scopes.
Local and global scope:
Variables created within a function and accessed only within that function are local scope.
Variables that are created outside a function and can only be accessed outside the function are globally scoped.
local:
<!DOCTYPE html><html><body><?php
functioncode(){
$a=1;// 本地范围echo"<p>Variable a inside function is: $a</p>";}code();// Using variable 'a' outside the function will generate ERRORecho"<p>Variable a outside function is: $a</p>";?></body></html>
Output:
Global:
<!DOCTYPE html><html><body><?php$a=1;// GLOBAL SCOPEfunctioncode(){
// Using variable 'a' inside this function will generate ERRORecho"<p>Variable a inside function is: $a</p>";}code();echo"<p>Variable a outside function is: $a</p>";?></body></html>
Output:
Global keywords:
To access global variables from within a function, use the Global keyword. To do this, you must use the Global keyword before the variable within the function.
All global variables are stored in an array named $GLOBALS[index]. This index holds the name of the variable.
When the function executes, all its variables are deleted. Sometimes we need a local variable and don't want to delete it for future purposes. To do this, use the Static keyword when creating the variable. Then only every time you run the function, the information will still be retained in the variables contained the last time the function was called.
The arrangement of characters is a String string. It can be any text within single quotes (' ') or double quotes (" ").
<!DOCTYPE html><html><body><?php$a="Hello Everyone. Thanks For Choosing Code Projects";$b='Hello Everyone. Thanks For Choosing Code Projects';echo$a;echo"<br>";echo$b;?></body></html>
Output:
Integer:
It is a non-decimal number between -2,147,483,648 and 2,147,483,647. Integers must contain at least one digit, which can be negative or positive, and cannot have any decimal points. Integers can be described in three formats: decimal (starting at 10), hexadecimal (starting at 16 - prefixed by 0x) or octal (starting at 8 - prefixed by 0).
It produces 2 possible states. They are TRUE or FALSE. It is mainly used for conditional testing.
$a = true;
$b = false;
Array:
Arrays store many values in a single variable.
n a m e = a r r a y ( “ j o h n ” , “ j o h n n y ” , ” h a r r y ” ) ; / / h e r e ‘ name = array(“john”, “johnny”,”harry”); //here ‘ name=array(“john”,“johnny”,”harry”);//here‘name’ is an array
It stores data and information about what to do with that data. In PHP, an object must be created explicitly.
First create an object class. To do this, you must use the class keyword.
classA class is a structure containing properties and methods.
<!DOCTYPE html><html><body><?php
classCodes{
functionCodes(){
$this->lang="PHP";}}// Create an object$prp=newCodes();// Show Object Propertiesecho$prp->lang;?></body></html>
Output:
Empty value NULL:
It can only have one value. That is NULL. A variable with no value assigned is a null data type. When a variable is created without a value, it is automatically Null.
Output:
ResourceResource:
It's not an actual data type. It stores references to PHP external functions and resources.
Database calls are an example of using resources.
Resource data will be returned when performing operations such as connecting to the database and opening external files. When we enable #### to connect to the database with the required configuration details, for example,