Variables and Data Types – PHP Basics

Variables and Data Types – PHP Basics

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.

Declare variables:

The '$' symbol is used to declare variables here.
<!DOCTYPE html>
 <html>
 <body>

<?php
 $text = "Code Projects";
 $a = 0;
 $b = 1;

echo $text;
 echo "<br>";
 echo $a;
 echo "<br>";
 echo $b;
 ?>

</body>
 </html>

Output:

Insert image description here

Output variables:

<!DOCTYPE html>
 <html>
 <body>

<?php
 $text = "code-projects.org";
 echo "Projects, Tutorial and More on :- $text";
 ?>

</body>
 </html>

Output:

Insert image description here

Display variable sum:

<!DOCTYPE html>
 <html>
 <body>

<?php
 $a = 10;
 $b = 20;
 $c = 30;
 echo $a + $b + $c;
 ?>

</body>
 </html>

Output:

Insert image description here

Variable scope

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
 function code() {
    
    
 $a = 1; // 本地范围
 echo "<p>Variable a inside function is: $a</p>";
 }
 code();

// Using variable 'a' outside the function will generate ERROR
 echo "<p>Variable a outside function is: $a</p>";
 ?>

</body>
 </html>

Output:

Insert image description here

Global:

<!DOCTYPE html>
 <html>
 <body>

<?php
 $a = 1; // GLOBAL SCOPE

function code() {
    
    
 // Using variable 'a' inside this function will generate ERROR
 echo "<p>Variable a inside function is: $a</p>";
 }
 code();

echo "<p>Variable a outside function is: $a</p>";
 ?>

</body>
 </html>

Output:

Insert image description here

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.
<!DOCTYPE html>
 <html>
 <body>

<?php
 $a = 10;
 $b = 20;

function code() {
    
    
 $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
 }

code();
 echo $b;
 ?>

</body>
 </html>

Output:

Insert image description here

Static keywords:

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.
<!DOCTYPE html>
 <html>
 <body>

<?php
 function code() {
    
    
 static $a = 10;
 echo $a;
 $a++;
 }

code();
 echo "<br>";
 code();
 echo "<br>";
 code();
 ?>

</body>
 </html>

Output:

Insert image description here

type of data:

Types include: String, Integer, Float, Boolean, Array, Object, NULL, Resource

String :

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:

Insert image description here

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).
<!DOCTYPE html>
 <html>
 <body>

<?php
 $a = 012345;
 var_dump($a);
 ?>

</body>
 </html>

Output:

Insert image description here

Float:

It is a number in exponential form or a number that contains a decimal point.
float ex. b = 1.11 ; / / b = 1.11; // b=1.11;//b is float
<!DOCTYPE html>
 <html>
 <body>

<?php
 $b = 20.0123;
 var_dump($b);
 ?>

</body>
 </html>

Output:

Insert image description here

Floating point (Boolean):

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);//herename’ is an array
<!DOCTYPE html>
 <html>
 <body>

<?php
 $langs = array("PHP","C#","PYTHON","JAVASCRIPT");
 var_dump($langs);
 ?>

</body>
 </html>

Output:

Insert image description here

Object:

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
 class Codes {
    
    
 function Codes() {
    
    
 $this->lang = "PHP";
 }
 }
 // Create an object
 $prp = new Codes();

// Show Object Properties
 echo $prp->lang;
 ?>

</body>
</html>

Output:

Insert image description here

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:

Insert image description here

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,
#### //connection
#### $conn = mysqli_connect(localhost,"root","admin","pkr115");
This function will return the resource type data to be stored in the "$conn" variable.
We can retrieve the required list of data items from the database with reference to the created resource.

Guess you like

Origin blog.csdn.net/qq_37270421/article/details/133303342