PHP: Data Types and Variables

PHP: Data Types and Variables
Variable is a very important thing in a programming language. Since most of the programming process is to process the data stored in the variable.

variable name

There are some rules that need to be considered in making a variable in PHP is:
  1. Each variable is always preceded by a $ (dollar)
  2. Allowed three types of characters are letters, numbers and underscores
  3. The first character must be a letter or underscore
  4. If a variable name is two, there should be no spaces in between.
  5. Small and large letters will be different ( case sensitive )


Giving value to the variable

To assign a value to a variable, you can follow the formula as follows:

$variable_name = value of the data;


$value1=12; //type data integer
$value2=8.99; //type data float atau double
$value3=”Tes Nilai variabel string”; //type data string
print “Tipe data integer : $value1<br>”;
print “Tipe data double : $value2<br>”;
print “Tipe data string : $value3”;

The result:

PHP: Data Types and Variables

Single and Double Quote

To display the data type string, we can use single or double quotes. To display double quotation marks itself way is as follows. Try the following data scripts.


print “Bandung is known as ” the Flower City “”;?>


Together with a variable constant but its value is fixed or permanent. By providing a constant value on the data type, we will not be able to vary the value. If we use data that are long and hard to remember constants should be used. The formula used is as follows:


An example of its use is as a script the following courses:

define(“Hasilnya”,”Keliling Lingkaran”);
printf(“Jari-jari lingkaran adalah $jarijari<br>”);
printf(“Nilai phi=%s<br>”,phi);

the result:

PHP: Data Types and Variables

Constants from another file

To call a constant value in other files You can use the following ways:

require(“namafile.php”); or includ(“namafile.php”);



define(“Hasilnya”,”Keliling Lingkaran”);


print”Jari-jari = $jarijari”.GantiBaris;
print”Nilai phi”.phi.GantiBaris;
print Selesai;

The result:

PHP: Data Types and Variables

See Variable Data Types

There are some functions provided by PHP to detect the data type of a variable, such as:

  • is_string (variable_name) , ascertain whether the data type of a variable is string
  • is_int (variable_name) , to ascertain whether a variable is an integer data type
  • is_numerical (variable_name) , ensure is variable data type is a numerical / figures
  • is_double (variable_name) , ascertain whether the variable data type is a double
  • is_array (variable_name) , ascertain whether the data type of the variable is an array
  • is_bool (variable_name) , ascertain whether the data type is a boolean variable
Function GetType (variable_name) ; used to determine the variable data used
<? php 
$ number = 78; 
$ font = “Learning PHP fast”; 
$ number = 22.78; 
print “The variable \ $ count”; 
if (is_int ($ number)) 

    print “True, integers most”; 


    print “Wrong, not integers most”; 

Print “most”; 
print “Are variable \ $ letters of type string? Reviews”; 
if (is_string ($ letter)) 

    print “True, most string-type”; 


    print “Wrong, not the type string”; 

Print “most”; 
print “Are variable \ $ number of type boolean? <

print “True, this variable of type boolean most”; 

Else { 
    print “Wrong, but not of type boolean type” .gettype ($ number); 

The result:
PHP: Data Types and Variables

See Values ​​and Data Types Variables

To determine the value and data type of a variable, you can use the following functions:


echo “Variabel \$angka adalah “;
echo var_dump($angka).”<br>”;
echo “Variabel \$huruf adalah “;
echo var_dump($huruf).”<br>”;
echo “Variabel \$bilangan adalah “;
echo var_dump($bilangan).”<br>”;

The result:

PHP: Data Types and Variables

Next, you can learn  Introduction to PHP Operator.

Leave a Reply

Your email address will not be published. Required fields are marked *