Learn Basic PHP Programming Language With Logic
PHP is a scripting language that integrates with HTML and runs on a server side. This means that all the syntax we provide will be fully executed on the server while sent to the browser only the result only.
Here are some examples of basic PHP scripts that can be learned.
Example1.php file:
<html>
<head>
<title> Simple Example </ title>
</head>
<body> <?php
echo("Hallo How are you ? I'm Learning PHP script");
?>
</body> </ html>
VARIABLE
In PHP each variable name begins with a dollar sign($). For example the variable name a in PHP is written with $a. The type of a variable is determined when the program runs and depends on the context used.
Example2.php file:
<?php
$a = "5"; $b = "2"; $result = $a + $b;
echo($results);
?>
The result is: 7
Example3.php file:
<?php
$a = "5"; $b = "2"; $result = $a. $b;
echo($results);
?>
The result is: 52
CONTROL STRUCTURE
IF
IF Construction is used to perform conditional execution of a statement. The way of writing is as follows:
If(condition)
{
Statement
}
or:
If(condition)
{
Statement
}
Else
{
Another statement
}
or:
If(first condition)
{
First statement
}
Elseif(second condition)
{
Second statement
}
Else
{
Another statement
}
Example5.php file:
<?php
$a = 4; $B = 9; If($a> $b)
{
echo("a is bigger than b");
}
Elseif($a <$b)
{
echo("a smaller b");
}
Else
{
echo("a is equal to b");
}
?>
The result is: a smaller b
WHILE
The basic form of the While statement is as follows:
while(terms)
{
statement
}
The meaning of statemant While is giving the command to run the statement under it repeatedly, as long as the conditions are met.
Example file6.php:
<?php $a = 1;
while($a <10)
{
echo($a);
$a ++;
}
?>
The result is: 123456789
FOR
How to write FOR statements are as follows:
For(expression1; expression2; expression3) statement
Expression1 indicates the initial value for a variable
Expression2 indicates the condition that must be fulfilled to run statemant expression3 shows the value added for a variable
Example file7.php:
<?php
For($a = 0; $a <10; $a ++)
{
echo("Value A ="); echo("$a");
echo("<br>");
}
?>
The result is:
Value A = 1
Value A = 2
Value A = 3
Value A = 4
Value A = 5
Value A = 6
Value A = 7
Value A = 8
Value A = 9
SWITCH
The SWITCH statement is used to compare a variable with multiple values and execute a particular statement if the value of a variable equals the value being compared.
Switch structure is as follows:
Switch(variable) case value: statement case value: statemant case value: statement
.
.
.
Example8.php file:
<?php $a = 2; Switch($a)
{
Case 1:
echo("The value of variable a is one");
break; Case 2:
echo("The value of variable a is two");
break; Case 3:
echo("The value of variable a is three");
break;
}
?>
The result is: The value of variable a is two
REQUIRE
Statement Require is used to read the value of variables and functions of another file. How to write a statement Require is:
Require(filename);
This Require statement can not be entered in a looping structure such as while or for. Because it only allows calling the same file only once.
Example9.php file:
<?php
$a = "I'm learning PHP";
function tulistebal($text)
{
echo("<b> $text </ b>");
}
?>
Example file10.php:
<?php
require("contoh9.php");
tulistebal("This is bold");
echo("<br>");
echo($a); ?>
INCLUDE
The include statement will include the contents of a particular file. include can be placed inside a loop such as in the for or while statement.
Example11.php file:
<?php
echo("-------------------------------------- <br>"); echo("PHP is a scripting language <br>"); echo("-------------------------------------- <br>"); echo("<br>");
?>
Example12.php file:
<?php
for($b = 1; $b <5; $b ++)
{
include("contoh11.php");
}
?>
So basic PHP along with its easy logic easy to understand, apologize if there is mistake.
No comments :
Post a Comment