PHP Tutorial: Data Types and Declaration of Variables & Strings in PHP

Last updated on Apr 29,2020 12.5K Views

PHP Tutorial: Data Types and Declaration of Variables & Strings in PHP

edureka.co

In the previous PHP Tutorial we learnt the various ways to differentiate the PHP code from the HTML code, so that it becomes easy for the PHP parser to interpret the code. In this post, let’s learn how to store information in the PHP script. Storing information is necessary so that you can use the value anywhere in the program.Variable is a name given to store values that is to be used in the program. In PHP, the variable names are preceded with the ‘$’ symbol.

Example:

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

Variable Naming:

There are certain rules that must be followed when naming the variables. They are:

The first step to use a variable, is to assign a value to it.

Assigning Variables:

Variable assignment is simple. Just write the name and add a single equal sign (=), and then expression that we want to assign to that variable.

Example: $pi=3 + 0.1489;

Reassigning Variables:

After assigning a value to the variable, if it has to be changed in the later stages of the program, you can also reassign the values to the same variable name.

Example:

$my_num_var = “this should be a number – hope it is reassigned later”;
$my_num_var = 5;

Unassigned Variables:

Many programming languages reports an error if you try to use the variables before they are assigned any value. But the PHP deals with such unassigned variables. In PHP, the default error-reporting settings allow you to use the unassigned variables without reporting any errors. If you would like to be warned about variables that have not been assigned,  the error-reporting level to E_ALL from the default level of error reporting.

This can be done in two ways:

Default Values:

When you do not pass values to the parameters in functions, PHP assigns a value by default, which is called the default value

The data to be stored and used in the PHP script can be of various types. In order to differentiate this, PHP provides various Data Types.

Data Types in PHP

The values stored and used in the PHP script are of different types. The Data Types define the type of data being used in the program. PHP has 8 Data Types. They are:

These data types are classified into 2 major types:

Simple Data Types:

Integers:

Integers are of the simplest type. They correspond to simple whole numbers, both positive and negative.

Example:

$int_var = 6789;
$another_int = -1245+134 //will zero

Doubles and Floating Point Numbers:

Real numbers (i.e., numbers containing a decimal point)

Example:

$first_double =568.998;
$second_double = 0.444;
$even_double =6.0;

Booleans:

Booleans are true – or – false values, which are used in control constructs like the testing portion of an if statement

Boolean Constants:

To use Booleans, PHP provides a couple of constants: TRUE and FALSE

Example:

If(TRUE)
Print();
Else
Print();

NULL:

NULL type, however, takes this to the logical extreme: The type NULL has only one possible value, which is the value NULL

Example:

$my_var = NULL;

A variable that has assigned to null has the following properties:

Apart from declaring numbers, PHP also supports “Strings” where sequence of characters are treated as a single unit.

Declarations of Strings in PHP

Strings are a sequence of character that are treated as one unit.Strings in PHP are declared in two ways:

Single Quoted String:

Here the statement present within the single quotes will be displayed as it is without any changes.

Example:

<?php
$string_variable = "name";
$literally = 'My $string_variable is Happy!
';
print($literally);
?>

Output:

My $string_variable is Happy!

Double Quoted String:

Here the statement present within the double quotes will be interpreted and the output of the program will be displayed.

Example:

<?php
$string_variable = "name";
$literally = “My $string_variable is Happy!
”;
print($literally);
?>
Output:

My name is Happy!

When we want to execute a block of statements repeatedly, functions are used.

Stay tuned for our next post on how to pass parameter to functions and the various built-in functions supported in PHP.

Got a question for us? Please mention them in the comments section and we will get back to you.

Related Posts:

Get Started with PHP & MySQL

PHP Tutorial: Differentiating PHP Code in HTML Script 

BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial