Skip to main content

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.
<? php 
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
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.

Comments

Popular posts from this blog

Contoh Program C++ : Konversi Bilangan Hexadesimal, Oktal , Biner.

Hai Pembaca FajarYusuf.Com yang pintar dan berwawasan tinggi, kali ini FajarYusuf.Com akan post Contoh Program C++ : Konversi Bilangan Hexadesimal, Oktal , Biner . Mari kita membuat program konversi bilangan yang sangat mudah di buat dengan menggunakan bahasa pemrograman C++. Yaitu dengan menggunakan type data long, untuk Hexadesimal dengan rumus : <<hex<<desimal (merubah dari bilangan Desimal ke Hexadesimal) , untuk Oktal dengan rumus : <<oct<<desimal (merubah dari bilangan Desimal ke Oktal). Dan untuk biner dilakukan pembagian 2. Contoh Coding Program C++ Konversi Bilangan: #include<iostream> using namespace std; int main() { long desimal, pembagi=1073741824, bit; cout<<"Nilai Desimal : "; cin>>desimal; cout<<"Hexadesimal = "<<hex<<desimal<<endl; cout<<"Oktal = "<<oct<<desimal<<endl;
Les rumeurs sur les rencontres avec RM ont d’abord été propagées par un YouTuber, qui a déclaré que RM sortait avec une femme du cercle non-célébrité, mais qu’elle était riche.  La rumeur est née à cause d’une photo de RM avec un caniche, qui aurait appartenu à la femme. Les rumeurs se sont rapidement propagées sur internet et ont fait se demander nombre de fans: est-il vrai que le leader du groupe BTS a un amant? RM a ensuite répondu aux rumeurs via Weteverse jeudi 30/12/201. “Je ne connais pas du tout cette personne. Le caniche est le caniche de mon ami,” a déclaré RM, cité à partir d’un t weet  appartenant à @btsinthemoment. namjoon weverse moment 🐨 i dont know the person at all and the poodle is my friends poodle ^^; pic.twitter.com/okz0AIpVnc — 윤서⁷ (@btsinthemoment) December 31, 2021 Big Hit a également nié les allégations dans une déclaration publiée par  AllKPop .  “Les rumeurs de rencontres sur RM ne sont pas vraies,” a déclaré Big Hit, vendredi (31/12). Et apparemment, dans

Opisyal na Inilabas ni Adele ang Bagong Album na "30"

Opisyal na inilabas ni Adele ang kanyang pinakabagong album na pinamagatang “30” noong Biyernes (11/19/2021).  Nakatanggap ang bagong album ng mgapositibong pagsusuri mula sa mga kritiko ng musika. Ayon sa Rolling Stone , ang “30” ay ang pinakamahusay na album ni Adele. Mayroong 12 kanta sa “30” album na ito, isa na rito ang hit single na pinamagatang  Easy On Me .  Sa simula ng album, maririnig mo ang  Strangers By Nature— isang kantang puno ng patula na liriko. At sa dulo ay ang kantang  Love Is A Game , na nagsasalaysay ng karanasan ni Adele sa sakit na dulot ng pag-ibig. Sa  Metacritic  , nakatanggap ang bagong album ni Adele ng  metascore  na 89% mula sa 12  review  at nasa numero 8 sa listahan ng  Best Albums ng 2021 .  Habang nasa Apple Music Indonesia, ang LP “30” ay unang niraranggo sa Mga   Nangungunang Album. Adele sa British Vogue magazine (instagram.com/britishvogue). Ang album na ito ay inspirasyon ng diborsiyo ni Adele kay Simon Konecki. Sa isang pakikipanayam sa