PHP Common Functions (PCF)

String Functions

String functions are used to manipulate strings for various needs. Here we will discuss some string functions that are often used in creating web application programs.

AddSlashes 

Used to add a backslash character ( \ ) to a string. This is important to use in query strings for databases, for example in MySQL. Some characters that will be added with backslashes are the single quote character ( ' ), the double quote character ( " ), the backslash ( \ ) and the NULL character. 

Syntax:

addslashes(string)

StripSlashes 

Used to remove the backslash character ( \ ) in a string.

Syntax:

string stripslashes(string)

Crypt 

Used to encrypt with DES method a string. This function is often used to randomize password string before it is stored in the database. In using this crypt function, the string parameter 'salt' can be added. This 'salt' parameter is added to determine the randomization basis. The 'salt' string consists of 2 characters. If the 'salt' string is not added to the crypt function, PHP will determine the 'salt' string itself randomly.

Syntax:  

crypt(string  [ , salt ] )

Echo 

Used to print the contents of a string or argument.

Syntax:  

echo( string argumen1, string argumen2 , ....)

Explosion 

Used to break up a string based on a certain separator and put the results into an array variable.

Syntax:  

explode(string pemisah , string [, int limit] )

Example:  

$namahari = "minggu senin selasa rabu kamis jumat sabtu";
$hari = explode(" ", $namahari);

Implode 

The use of this function is the opposite of the explode function. The implode function is used to produce a string from each element of an array. The resulting string is separated by a predetermined string. 

Syntax:  

implode(string pemisah , array)

Strip_Tags 

Used to remove HTML tag codes in a string.

Syntax:  

striptags(string [, string tags yang tidak dihilangkan] )

StrLen 

Used to count the number of characters in a string.

Syntax:  

strlen(string)

StrPos 

Used to find the first position of a sub string in a string. This function is usually used to find a sub string in a string.  

Syntax:  

strlen(string , sub string)

Str_Repeat 

Used to repeat the contents of a string. 

Syntax:  

str_repeat(string , int jumlah perulangan)

StrToLower 

Used to change a string to lowercase.

Syntax:  

strtolower(string)

StrToUpper 

Used to change a string to uppercase.

Syntax:  

strtoupper(string)

SubStr 

Used to take a sub string of a certain length from a string at a certain position.

Syntax:  

substr(string, int posisi , int posisi)

Example:  

substr("abcdefg",0,3);  // mengasilkan string "abc" substr("abcdefg",3,2); // menghasilkan string "de"

SubStr_Count 

Used to count the number of sub strings in a string.

Syntax:

substr_count( string , string substring)

Example:  

substr_count("This is a test","is");  // menghasilkan nilai 2

UCFirst 

Used to change the first character in a string to uppercase.

Syntax:  

ucfirst(string)

UCWords 

Used to change the first character of each word in a string to uppercase.

Syntax:  

ucwords(string)

Date Function

Used to retrieve the date and time. The result of this function is a string containing the date/time according to the desired format. The formats recognized in this date function are as follows: 

  • a - "am" or "pm"
  • A - "AM" or "PM"
  • B - Swatch Internet time 
  • d - day of the month, 2 digits with leading zeros; i.e. "01" to "31" 
  • D - day of the week, textual, 3 letters; ie "Fri" 
  • F - month, textual, long; i.e. "January" 
  • g - hour, 12-hour format without leading zeros; i.e. "1" to "12" 
  • G - hour, 24-hour format without leading zeros; i.e. "0" to "23" 
  • h - hour, 12-hour format; i.e. "01" to "12" 
  • H - hour, 24-hour format; i.e. "00" to "23" 
  • i - minutes; ie "00" to "59" 
  • I (capital i) - "1" if Daylight Savings Time, "0" otherwise. 
  • j - day of the month without leading zeros; i.e. "1" to "31" 
  • l (lowercase 'L') - day of the week, textual, long; i.e. "Friday" 
  • L - boolean for whether it is a leap year; i.e. "0" or "1"
  • m - month; ie "01" to "12" 
  • M - month, textual, 3 letters; ie "Jan" 
  • n - month without leading zeros; i.e. "1" to "12" 
  • s - seconds; ie "00" to "59" 
  • S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd" 
  • t - number of days in the given month; i.e. "28" to "31" 
  • T - Timezone setting of this machine; ie "MDT" 
  • U - seconds since the epoch 
  • w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday) 
  • Y - year, 4 digits; ie "1999" 
  • y - year, 2 digits; ie "99" 
  • z - day of the year; i.e. "0" to "365" 
  • Z - timezone offset in seconds (ie "-43200" to "43200").

Syntax:  

date(string format)

Example:  

date("Y-m-d");  // menghasilkan "2001-07-28"  date("l, j F Y"); // menghasilkan "Saturday, 28 July 2001"  date("H:i:s"); // menghasilkan "20:15:07"

Mail Functions

Used to send e-mail to a specific e-mail address.

Syntax:  

mail(string tujuan , string subject , string isi  [, string header] );

Example: 

$pengirim = "From: saya@email.com";
$tujuan = "anonkuncoro@yahoo.com";
$subject = "Pemberitahuan";
$isi = "Ini adalah percobaan pengiriman e-mail dengan menggunakan PHP"; mail($to,$subject,$isi,$pengirim);

Introduction to PHP

PHP is a scripting language that is integrated with HTML and runs on the server side. This means that all the syntax we provide will be fully executed on the server while only the results are sent to the browser.

Example file1.php:

<html>
<head>
<Contoh Sederhana title>
</head> </title>
<body> <?php
echo("Hallo apakabar? Nama saya PHP script");
?>
</body>
</html>

the result is:

VARIABLE 

In PHP, every variable name begins with a dollar sign ($). For example, the variable name a in PHP is written as $a. The type of a variable is determined at program execution time and depends on the context in which it is used.

Example file2.php:

<?php
$a="5";
$b="2";
$hasil=$a+$b;
echo($hasil);
?>

the result is:

Example file3.php:

<?php
$a="5";
$b="2";
$hasil=$a.$b;
echo($hasil);
?>

the result is:

CONTROL STRUCTURE

IF 

The IF construct is used to execute a statement conditionally. The way to write it is as follows:

if (syarat)
    {
    statement
    }

or

if (syarat)
    {
    statement
    }
    else
    {
    statement lain
    }

or

if (syarat pertama)
    {
    statement pertama
    }
    elseif (syarat kedua)
    {
    statement kedua
    }
    else
    {
    statement lain
    }

Example file5.php:

<?php
$a=4; $b=9; if ($a>$b)
    {
    echo("a lebih besar dari pada b");
    }
elseif ($a<$b)
    {
    echo("a lebih kecil b");
    }
else
    {
    echo("a sama dengan b");
    }
?>

The result is:

WHILE 

The basic form of the While statement is as follows:

while (syarat)
    {
    statement
    }

The meaning of the While statement is to give a command to run the statement below it repeatedly, as long as the conditions are met.

Example file6.php:

<?php
$a=1; while ($a<10)
    {
    echo($a);
    $a++;
    }
?>

The result is:

FOR 

The way to write the FOR statement is as follows:

for (ekspresi1; ekspresi2 ; ekspresi3)
statement

//ekspresi1 menunjukkan nilai awal untuk suatu variable
//ekspresi2 menunjukkan syarat yang harus terpenuhi untuk menjalankan statemant
//ekspresi3 menunjukkan pertambahan nilai untuk suatu variable

Example file7.php:

<?php
    for ($a=0;$a<10;$a++)
    {
    echo("Nilai A = ");     echo("$a");
    echo("<br>");
}
?>

The result is:

SWITCH 

The SWITCH statement is used to compare a variable with several values ​​and execute a certain statement if the variable value is the same as the value being compared. The Switch structure is as follows:

switch (variable)
case nilai:
statement
case nilai:
statemant
case nilai:
statement
    .
    .
    dst...

Example file8.php:

<?php
$a=2; switch($a)
    {
    case 1:
    echo("Nilai variable a adalah satu");
    break;      case 2:
    echo("Nilai variable a adalah dua");
    break;      case 3:
    echo("Nilai variable a adalah tiga");
    break;
    }
?>

The result is:

REQUIRE 

The Require statement is used to read variable values ​​and functions from another file. The way to write a Require statement is: 

require(namafile);

This Require statement cannot be included in a looping structure such as while or for. Because it only allows calling the same file once.

Example file9.php:

<?php
$a="Saya sedang belajar PHP";

function tulistebal($teks)
{
echo("<b>$teks</b>");
}
?>

Example file10.php: 

<?php
require("contoh9.php");
tulistebal("Ini adalah tulisan tebal");
echo("<br>");
echo($a);
?>

The result is:

INCLUDES 

The Include statement will include the contents of a particular file. Include can be placed inside a loop, for example in a for or while statement.

Example file11.php:

<?php
echo("--------------------------------------<br>"); echo("PHP adalah bahasa scripting<br>"); echo("--------------------------------------<br>"); echo("<br>");
?>

Example file12.php:

<?php
for ($b=1; $b<5; $b++)
    {
    include("contoh11.php");
    }
?>

The result is:

By Anon Kuncoro Widigdo Copyright © 2003 IlmuKomputer.Com

String Handling In PHP

String handling is a collection of PHP functions that are useful for manipulating strings. There are many uses that can be obtained by using these functions, for example: 

  • Search for words in a website
  • User input checking
  • Format files for special purposes (e.g. email).
  • Etc.

A. Overview of Regular Expressions 

To be able to use string handling functions properly, we need to master the technique of creating sentence patterns. For example, a valid email address (e.g.  endy@ngoprek.org ) always has the following pattern:

One or more letters/numbers, followed by the @ sign then followed by one or more letters/numbers, separated by a period, then ending with one or more letters/numbers.

This pattern can be expressed with a set of codes as follows:

| Pola                           | kode        |
|--------------------------------|-------------|
| Harus di awal kata             | ^           |
| Huruf                          | A-Za-z      |
| Angka                          | 0-9         |
| Huruf atau angka               | [A-Za-z0-9] |
| Semua jenis karakter           | .           |
| Berjumlah satu atau lebih      | + atau {1,} |
| Berjumlah nol atau lebih       | * atau {0,} |
| Berjumlah tiga sampai sepuluh  | {3,10}      |
| Diikuti dengan @               | @           |
| Tanda titik                    | \.          |
| Harus berada di akhir kalimat  | $           |

The above email address pattern can be expressed with one line of code as follows.

^.+@.+\..+$

Or we can limit the email addresses used by users to only accept com, net, or edu domains by using the following pattern: 

^.+@.+\.((com)|(edu)|(net))$

B. Use of Regular Expressions 

To demonstrate the capabilities of string handling, we will use an input form that will validate the email address and phone number entered by the user.

CekMail.htm looks like the image below:

The input entered by the user will be checked by the CekMail.php script which contains the following code:

<?
// validasi alamat email $polaEmail = "^.+@.+\..+$"; if(!eregi($polaEmail, $email)){   echo("Masukkan alamat email yang valid, misal :
endy@ngoprek.org");
}else{
  echo("Alamat email valid");
}

// validasi no telp
$polaTelp = "^\+[0-9]{2}-[0-9]+$"; if(!eregi($polaTelp, $telp)){
  echo("Masukkan nomer telepon yang valid, misal : +62-
315054307"); }else{
  echo("No telp valid");
}

?>

The function used to check email in the script above is eregi. It accepts input in the form of the desired pattern and the string to be checked. This function will produce a true value if the pattern matches and false if the pattern being searched for is not in the input string.

Understanding PHP Classes and Objects

Software applications are created to solve real-life problems. In the design process, several approaches were used.  

  1. Sequential Programming
  2. Structured Programming
  3. Object Oriented Programming.

A. Concept of class and object 

To understand classes and objects, we will visualize an address book application. In an address book, the main component involved is contacts. The contact component is called a class.

Class is a definition (way of describing) an object. Object is a real object that exists in the training session, including: Contact: Charlie, Budi, Ani

This relationship can be expressed in technical terms as follows: An object is an instance of a class.

The class is defined with the following code: 

<?
class Contact{
}
?>

Objects of the Contact class are created with the following code:

$ani = new Contact();
$budi = new Contact();
$charlie = new Contact();

B. Methods and properties 

More details about the Contact class. All contacts, whether Ani, Budi, or Charlie, have the same characteristics. They all have a full name, phone number, and address. These characteristics are known as properties. Properties are translated into code as:

<?
class Contact{
var $namaLengkap;
var $telp;
var $alamat;
}
?>

Each object has different values ​​for each property. It is also often said: objects have different states from each other. A code sample that illustrates this condition is:

$ani = new Contact();
$ani->namaLengkap = "Ani Malia";
$ani->telp = 528;
$ani->alamat = "Ragunan";

$budi = new Contact();
$budi->namaLengkap = "Budi Man";
$budi->telp = 456;
$budi->alamat = "Bandung";

$charlie = new Contact();
$charlie->namaLengkap = "Charlie Charmless";
$charlie ->telp = 123;
$charlie ->alamat = "USA";

In addition to storing characteristics, classes can also perform activities. This is called a method. For example, the Contact class can perform the activity of calling another contact. This concept is translated into the following code:

namaLengkap);
  }
}
?>

the code is executed as follows:

$ani = new Contact();
$ani->namaLengkap = "Ani Malia";
$budi = new Contact();
$budi->panggil($ani);

and produces the following output:

Calling Ani Malia

Understanding PHP Variable Lifetime

A. Scope 

Scope, lifetime, visibility are various terms that describe where a variable can be used in a program. PHP recognizes two types of scope, local and global. Local variables can only be used in the block where they are declared. Global variables can be used anywhere in the program after they are declared and initialized.

Local  

To better understand local scope, pay attention to the script below: 

<? function testVar(){
  $a = 3;
}
echo($a); // error -- variabel $a tidak dikenali
?>

The $a variable is only valid within the testVar function, so it cannot be accessed outside the function.

Global  

Global variables are declared outside the function and can be used anywhere in the program. Global variables do not apply inside the function unless called with the global keyword. Example of use:  

<? $a = 4;
function testVar2(){
  echo($a);     // -- tidak menghasilkan apa-apa   global $a;
  echo($a);     // -- menampilkan 4
}
?>

B. Passing variables

By Value 

Variables are passed into a function by passing by value. Pass by value makes a copy of the original variable. Thus the original variable is not affected. For more details, consider the following example: 

function tambahSatu($angka){
  $angka++;
}

The function will be used as follows:

<? $a = 7; tambahSatu($a);
echo("Nilai a = " . $a); ?>

The above code will produce the following output:

Value of a = 7

because the number variable in the function ends its life when the function finishes executing. And the original a variable remains at 7. To produce the desired effect, we can use pass by reference.

By Reference 

In pass by reference, we insert the original variable into the function. This technique is done in the following way: 

function tambahSatu(&$angka){
  $angka++;
}

The function will be used the same as the example above:

<? $a = 7; tambahSatu($a);
echo("Nilai a = " . $a);
?>

The above code will produce the following output:

Value of a = 8

PHP Function Concept

A function is a collection of statements created with the aim of completing a particular task.

1. Return value and parameters

Pay attention to the following code:

function add($a, $b){   return $a + $b;
}

The simple function above will receive input in the form of two numbers. Then the two numbers will be added together, and the result will be returned to the function caller. The value returned is called the return value. While the values ​​entered into the function ($a and $b) are called parameters.

2. Function declaration

There are several main things to note in function declaration in PHP. ? function name 

  • parameter
  • function body.

Example function:

function addNumber($x,  $y)
{
z = x + y;
echo(z);
}

function jumlahkanlah(int x,  int y)
{
z = x + y;
return z;
}

Consider the example of the addNumber function above. Line one is the function declaration. The declaration contains:  

  • keyword function
  • function name
  • parameter.

Parameters are values ​​entered into a function to be processed to produce output. The function name is determined by fulfilling the following rules:  

  • It cannot be the same as a function that already exists in PHP.
  • May only consist of letters, numbers and underscores
  • Cannot start with a number.

function 4uOnly(){} // should not

Other programming languages ​​support overloading, which means a function can have the same name and different results, provided the parameters are different. PHP does not support overloading. So, we cannot use a name that has been used before.

3. Implementation of function 

Example function:  

function addNumber($x,  $y)
{
z = x + y;
echo(z);
}

function jumlahkanlah(int x,  int y)
{
z = x + y;
return z;
}

Take a look at the addNumber function example above. Pay attention to lines 2 through 4.

  • The function body is delimited by a pair of { and }
  • The function body contains instructions that the computer must carry out to produce the desired output.
  • Line 3 tells the computer to create a variable named z whose contents are the sum of x and y.
  • x and y are obtained from the input provided by the user. 
  • Line 4 tells the computer to display the results of the calculation on the screen.
  • Distinguish it from line 4 in the function sum which instructs the computer to display the calculation results on the screen.

Example of function usage:

hasil = jumlahkanlah(4,5); addNumber(4,5);

Migration in PHP

break

Break is used in looping to stop a loop. For more details, see the following code:

<?
// melakukan break pada $i == 2 for($i = 0; $1<5; $i++){   if($i == 2){     break;   }
  echo("Nilai i : $i <br>");
}
echo("Loop Selesai");
?>

This code will break when i is 2, so it will produce the following output:

Value i : 0
Value i : 1
Loop Complete

continue 

continue is used to skip one iteration/round in a loop sequence. For more details, we will modify the code above.

<?
// melakukan continue pada $i == 2 for($i = 0; $1<5; $i++){   if($i == 2){     continue;   }
  echo("Nilai i : $i <br>");
}
echo("Loop Selesai");
?>

The code will break when i is 2, which will produce the following output:

Value i : 0
Value i : 1
Value i : 3
Value i : 4
Loop Complete

return 

The return command is used to instruct the code to exit the function. We will study the function in more depth in the next section. For now, the function will only be used to explain the return. Look at the code below:

<? function testReturn(){   for($i = 0; $1<5; $i++){
    // melakukan return pada $i == 2
if($i == 2){
        return;
    }
    echo("Nilai i : $i <br>");
  }
  echo("Loop Selesai");
}

//jalankan function testReturn();
echo("Function selesai");
?>

the output is: 

Value of i : 0
Value of i : 0
Function Completed

Note that the Completed Loop is not executed. This indicates that after the return is executed, the program immediately exits the function and executes the command after the function, namely

echo("Function Selesai");

exit

exit is used to stop the entire php script. For more details, we will modify the code above to be as follows:

<? function testExit(){   for($i = 0; $1<5; $i++){
    // melakukan return pada $i == 2     if($i == 2){       exit;     }
    echo("Nilai i : $i <br>");
  }
  echo("Loop Selesai");
}

// jalankan function testExit();
echo("Function selesai");
?>

the output is:

Value of i : 0
Value of i : 0
Function Completed

Note that the line

echo("Function selesai");

Not running

To see examples of using break, continue, return, and exit; add a view to control_flow.htm to look like the image below.

Add the following line of code to transfer.php

<? /*
variabel yang dibutuhkan
$mark -> tempat dilakukan perpindahan
$perintah -> perintah pindah : continue, break, return, exit
*/
function execute($tanda, $perintah){ for($i=0; $i<11; $i++){   if($i == $tanda){
    if($perintah == "continue"){       continue;
    }elseif($perintah == "break"){       break;
    }
    elseif($perintah == "return"){       return;
    }elseif($perintah == "exit"){       exit;
    }   }
  echo($i."<br>");
}
echo("Looping Selesai<br>");
}
execute($tanda, $perintah);
echo("Function execute selesai<br>");
?>

Loops in PHP

for 

Looping with for is also called a determinate loop, meaning a loop where the number of repetitions (iterations) has been determined at the start of the loop.  

There are several important parts of a for loop: 

  1. Initialization expression, executed once, when the loop starts. Usually this section is used to initialize the counter.
  2. Stop condition, is checked before each iteration is executed. If the condition is false, the iteration is stopped.  
  3. Iterative expression, performed after the iteration is executed. This section is usually used to increase the counter value. 
  4. The loop body, executed once per iteration, is the command we want to repeat.

The code example above will display the text Hello World in the browser 10 times. To see an example of using for, add a view to control_flow.htm to look like the image below.

Place the following line of code in forDemo.php

<?
$jumlah = 0;
for($i=0; $i<strlen($kata); $i++){   if(substr($kata, $i, 1) == $huruf){
    $jumlah ++;
  }
}
?>

while

while loop is also known as indeterminate loop, meaning the number of loops is not determined at the beginning of the loop. while loop is simpler than for loop, because it only has two parts:

  1. Stop Condition.
  2. Loop body.

The stop condition is checked before each iteration is executed. As long as the stop condition is true, the commands in the loop body will be executed repeatedly. The iteration will be stopped if the stop condition is false.

Just like in the for loop, the loop body is executed once per iteration. The loop above will continue to run without stopping, because there is no command that changes the value of the stop condition.

do-while loop is a modification of the while loop. Its form can be seen in the following code sample:

do{
  // some statement
}
while (a == true)

To see an example of using while, use the same form as forDemo.

Add the following line of code to whileDemo.php

<?
$jumlah = 0; $i = 0;
while($i<strlen($kata)){
  if(substr($kata, $i, 1) == $huruf){
    $jumlah ++;
    $i++;
  }
}
?>

Branching in PHP

Branching, or often called decision-making, allows an application to examine the contents of a variable or the result of an expression calculation and take appropriate action. There are two types of branching, chosen based on the examination criteria and the number of available choices.

if -- else

The if-else construction can be explained as follows:

if(condition)
{
 // statement 1 goes here
} else
{
 // statemant 2 goes here
}
// statement 3 goes here

Program flow: 

  1. Condition will be checked
  2. If it is true, statement 1 will be executed
  3. If the value is false, statement 2 will be executed. 4. Statement 3 will be executed.

if - elseif - else

For more than two choices, PHP provides the if-elseif-else construct.

if(condition1)
  {
     // statement 1
  }
  elseif(condition2)
  {
     // statement 2
  }   else
  {
     // statement 3
  }
  // statement 4

Program flow: 

There are 3 possible program flows:

If condition 1 evaluates to true: 

  1. Statement 1 is executed
  2. Statement 4 is executed 

If condition 1 is false, and condition 2 is true: 

  1. Statement 2 is executed
  2. Statement 4 is executed 

If condition 1 and condition 2 are false: 

  1. Statement 3 is executed
  2. Statement 4 is executed.

An example of using if-else can be seen by following the example below. Create two files, control_flow.htm and ifDemo.php. control_flow.htm has the following appearance:

ifDemo.php contains the following code listing:

<?php
// ifDemo.php
// digunakan bersama control_flow.htm
$kelabu = "#303030";
$putih = "#FFFFFF";

if($kelamin1 == "Pria"){
  $latar1 = $kelabu;
}else{
  $latar1 = $putih;
}
if($kelamin2 == "Pria"){
  $latar2 = $kelabu;
}else{
  $latar2 = $putih;
}
if($kelamin3 == "Pria"){
  $latar3 = $kelabu;
}else{
  $latar3 = $putih;
}
if($kelamin4 == "Pria"){
  $latar4 = $kelabu;
}else{
  $latar4 = $putih;
}

$output = "
<table border=1>
    <tr>
      <td background=$latar1>$nama1</td>
    </tr>
    <tr>
      <td background=$latar2>$nama2</td>
    </tr>
    <tr>
      <td background=$latar3>$nama3</td>
      </tr>
    <tr>
      <td background=$latar4>$nama4</td>
    </tr>
</table> ";
echo($output);
?>

switch -- case 

Switch construction can be explained as follows:

switch(a){   case 1;
    // statement 1 goes here     break;   case 2;
    // statement 2 goes here     break;   case 3;
    // statement 3 goes here     break;   default;
    // statement 4 goes here     break;
}
// statement 5 goes here

Program flow: 

1. Variable a is checked 

2. Statement is executed 

    a) If a == 1, statement 1 is executed 
    b) If a == 2, statement 2 is executed 
    c) If a == 3, statement 3 is executed 
    d) If a does not satisfy 2a - 2c, statement 4 is executed

3. Statement 5 is executed 

The break keyword plays an important role here. Its function is to prevent fall-through, compare with the following program (break in line 5 is omitted).

switch(a){   case 1;
    // statement 1 goes here   case 2;
    // statement 2 goes here     break;   case 3;
    // statement 3 goes here     break;   default;
    // statement 4 goes here     break;
}

Program flow:  

1. variable a is checked 

    2a. If a == 1, statement 1 is executed, then execute statement 2. 
    2b. If a == 2, statement 2 is executed 
    2c. If a == 3, statement 3 is executed 
    2d. If a does not satisfy 2a - 2c, statement 4 is executed

2. Statement 5 is executed 

The difference is in step 2a. Compare it with the first listing. To see an example of using the switch -- case, add a view to control_flow.htm to look like the image below.

Create the switchDemo.php file as follows:

<?php switch($bulan){
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :      $hari = 31;
break;
case 4 :
case 6 :
case 8 :
case 11 :      $hari = 30;
break;
case 2 :
if(($tahun%4) == 0){
      $hari = 29;
    }else{
      $hari = 28;
    }
}

Understanding PHP Control Flow

Control flow in Indonesian can be interpreted as control flow. The real meaning of control flow is how the order of command execution in the program. For example, in the function:

function testFlow(){
       int a = 5;
       echo(a);
  }

The first command executed is to fill the value 5 into the variable a. The second command executed is to display the value stored in the variable a (in this case 5) to the browser.

Simply put, it is a function mechanism for managing the order of execution.

The control flow above is a simple example. Some of the control flows available in PHP: 

  • Branching
  • Looping
  • Displacement (jumping)

How to Enable PHP Error Reporting

This tutorial is for those who are experiencing Internal Server Error. So your website is down and you are struggling to figure out why. You see Error 500 and don’t know what to do next. If you try googling error 500 it won’t give you the answer you are looking for. The error is too general and doesn’t tell you the real problem. What you need is to figure out what the real problem is.

At 000webhost all PHP errors are hidden by default.

Enable PHP Error Display

  1. Open your website with a file manager, for example FTP Server, FileZilla Client or the file manager on your hosting.
  2. Open the public_html directory
  3. Create a .htaccess file (if one doesn't exist)
  4. Add this line: php_value display_errors 1
  5. Save the file.

Now when your website has a problem you will see what the problem is and maybe google will crawl it too.

Immediately remove php_value display_errors 1 when the problem is solved.


Post a Comment

Previous Next

نموذج الاتصال