Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

Pages

Learning PHP: Basic PHP Syntax that You Must Understand

PHP Programming Tutorial for Beginners
We have learned to create programs hello_world.phpwith code like this:
<?php 
echo "Hello World!";
?>
This is the simplest example of a PHP program.
The program only serves to display text Hello World.
But…
What do you mean <?phpand ?>?
What is echo?
... and why should it be written like that?
If you are just beginning to learn programming, you might ask that question.
This is natural.
Because I was like that too ðŸ˜„.
Therefore ... to answer all these questions, we must understand the basic syntax of PHP.
What is syntax?
Syntax is the rule for writing program code.
Basically every programming language is the same, the difference is the syntax and features.

The Most Basic PHP Program Structure

This is the most basic form of a PHP program:
<?php

echo "Hello World!";
Information:
  • <?phpthis is the opening of the PHP program. This opening is required in every PHP program.
  • echo "Hello World!"; is a statement or command to display text.
Wait a minute…
Why isn't it closed with ?>?
The actual lid is optional. Close the program is needed when we combine PHP code with HTML.
When we write PHP code in HTML, it is obligatory to make the program close.
Example:
<!DOCTYPE html>
<html>
    <head>
        <title><?php echo "Belajar PHP" ?></title>
    </head>
    <body>
        <?php
            echo "saya sedang belajar PHP<br>";
            echo "<p>Belajar PHP hingga jadi master</p>";
        ?>
    </body>
</html>
Consider the example above!
What will happen if we delete the PHP ( ?>cap ?
Of course the program will error.
Anyway, PHP is written in HTML, the file must be saved with an extension .phpnot .htmleven though it contains HTML and PHP.
Then there are also those who write like this:
<?php

echo "<html>";
echo "<head>";
echo "<title>Judul Web</title>";
echo "</head>";
echo "<body>";
echo "<h1>Selamat datang</h1>";
echo "</body>";
echo "</html>";
Now if this can not be closed, because the HTML code is written in the PHP syntax.
Still confused?

Writing Statement and Expression

Statement and expansion are instructions that will be given to the computer. Every statement and expression in PHP must end with a semicolon ( ;).
Example:
<?php
echo "ini statement 1";
echo "ini statement 2";
$a = $b + $c;
What if we forget to write a semicolon?
Yes of course the program will error.

PHP Case Writing Rules

PHP is a programming language that is case sensitive . That is, uppercase (capital) and lowercase letters will be distinguished.
Example:
<?php

$nama = "petanikode";
$NAMA = "dian";

?>
Variables $namaand $NAMAare two different variables. They are not the same.
Writing upper and lower case letters in the program must be considered, because it can cause errors if wrong.
I often find it, many are mistyped.
Example:
$umur = 19;

echo "Umur saya adalah $Umur";
See!
In the code above, we create variables $umurwith lowercase letters. Then when using the variable we are using $Umur.
Obviously this will cause an error.
To avoid this, we must be consistent in naming variables and constants.
Use variable names with lowercase letters and constants with uppercase letters.
Example:
const INI_KONSTANTA = 123;
$ini_variabel = 23;
$iniJugaVariabel = 49;
Uh, how come there are capital letters in $iniJugaVariabel?
This is called camelCase .
If the variable consists of two or more syllables, then we can separate it with capital letters or it can be the undescore .
To learn more about this, please read: Various cases in writing program code

Writing Comments in PHP

Comments are parts that the computer will not execute. Usually used for information, explanations, and program code documentation.
Comments in PHP can be written in two ways:
  1. Use tags //for single line comments;
  2. Use tags /*to comment more than one line.
Example:
<?php 

// ini adalah komentar
echo "Hello world";

/*
ini adalah komentar 
yang lebih dari satu 
baris
*/

?>

Writing Block Program

The program block is a collection of statements and expressions. The program block in PHP is wrapped in curly brackets { ... }.
Example:
if ($umur > 18){
    echo "Kamu sudah dewasa";
    echo "Selamat datang";
    echo "Kamu boleh minum kopi";
}
The program above is an example of an if code block that contains three statements.

0 Komentar untuk "Learning PHP: Basic PHP Syntax that You Must Understand"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top