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;

example:

<?php
$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 “”;?>



Permanent

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:

define(“constant_name”,”constant_value”);

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

<?php
define(“phi”,3.14);
define(“Hasilnya”,”Keliling Lingkaran”);
$jarijari=7;
$keliling=2*phi*$jarijari;
printf(“Jari-jari lingkaran adalah $jarijari<br>”);
printf(“Nilai phi=%s<br>”,phi);
printf(“%s=$keliling”,Hasilnya);
?>

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”);

Example: 

constant.php

<?php
define(“phi”,3.14);
define(“Hasilnya”,”Keliling Lingkaran”);
define(“GantiBaris”,”<br>”);
define(“Selesai”,”thanks”);
$jarijari=7;
$luas=phi*$jarijari*$jarijari
?>

constant1.php

<?php
require(“konstanta.php”);
print”Jari-jari = $jarijari”.GantiBaris;
print”Nilai phi”.phi.GantiBaris;
print”Luas=$luas”.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
See also  Checking Prime Numbers: PHP
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”; 

Else 

    print “Wrong, not integers most”; 

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

    print “True, most string-type”; 

Else 

    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:

<?php
$angka=98;
$huruf=”Fajaryusuf.com”;
$bilangan=34.7;

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.

Fajar (Ucup)

The owner and author of FajarYusuf.com and also someone who likes to share interesting information with others.

Leave a Reply

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