Heim > Artikel > Backend-Entwicklung > Variablen in PHP
The following article, variables in PHP, provides an outline for the various variables available in PHP. Each variable stores some kind of information where information is a value. This value can be a number, a string, boolean, array, an object, a resource, etc.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The variables declared store information. Therefore, there are certain things one must know about declaring variables in PHP.
From the previous, we know that PHP is a loosely typed language and we need not declare the type like whether the variable is of integer, or string or boolean type before using it as it happens in other languages. The type of the variable depends upon the value it stores. Let us learn through examples.
Here in the below example, we see that the height is a float value and the base is an integer value and based on these values, we have calculated the area of the triangle.
Code:
<?php // example to demonstrate the intialization of variables $height = 10.5; //float value $base = 50; //integer value // calculating area of a triangle $area_of_triangle = ($height * $base) / 2; // printing area of the triangle echo 'Area of the triangle is '. $area_of_triangle; ?>
Output:
The below code shows all valid and invalid ways of initializing the variables in PHP.
$5input = 'Demo';
$_input = 'Demo';
$input = 'Demo';
$_5input = 'Demo';
Variables store values. These values assigned to the variables define what type of variable it is. There are eight data types:
Let us learn each in detail.
An integer is a whole number. This integer can be positive or negative. (if no significant meaning it is positive) It compulsorily has at least one digit ranging from 0 to 9, with no comma or blanks. It does not have a decimal point. Integers have different notations like
optionally preceded with a sign either – or +
<?php //example to demonstrate an integer datatype $x = 6900; $y = 45; //var_dump tells us about the datatype and value of the input number var_dump($x); echo '<br>'; var_dump($y); ?>
Output:
A string is a sequence of characters or letters. A string can hold a sequence of numbers, special characters, arithmetic values also. It can be a combination of all, as well. To represent a string, we use single or double-quotes.
<?php //example to demonstrate string datatype $input = 'Apple'; echo '<br> $input is my favorite fruit'; echo "<br> $input is my favorite fruit"; ?>
Output:
This data type can hold one of two values: true or false, where true is 1, and false is blank.
<?php //example to demonstrate boolean datatype $input = true; // print true echo "<br> True is ".$input; $input_value = false; // print false echo "<br> False is ".$input_value; ?>
Output:
A number with a decimal point or an exponential form is called a floating-point number or type float.
<?php //example to demonstrate float datatype $input = 123.45; $input_value = 9.e5; var_dump($input); echo '<br>'; var_dump($input_value); ?>
Output:
An object is a data type that stores data. Along with data, it also stores information about the processing of the data. An object is declared explicitly by declaring a class. Class is defined with the class keyword. A Class is a structure that contains data members and data methods.
A class is instantiated, and the object is created, and through this object now we can access the members and methods of the class.
<?php //example to demonstrate object datatype class Subject{ //defining a string property public $string = "My favourite subject is Maths"; //defining a method that returns the string property function display() { return $this->string; } } //instantiating an object of a class $object = new Subject; echo $object->string; ?>
Output:
It is a collection of similar and dissimilar data types. An array is declared in the form of key-value pair.
<?php //example to demonstrate array datatype $directions= array('East','West','North','South'); var_dump($directions); echo '<br>'; echo $directions[2] echo '<br>'; echo $directions[0]; ?>
Output:
When no value is assigned to a variable and the variable is empty, we can use the NULL value.
<?php //example to demonstrate NULL datatype $input = 'Demo Test'; var_dump($input); echo '<br/>'; $input = NULL; var_dump($input); ?>
Output:
A resource a special variable related to an external resource which can be file handling, database connectivity or others
<?php //example to demonstrate resource datatype //establishing a connection to database with default values $connection = mysql_connect("localhost", "root", ""); var_dump($connection); ?>
Das obige ist der detaillierte Inhalt vonVariablen in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!