Well if you decided to start learning php then you should view this tutorial. Here i will tell you everything what you need to know for the beginning. Okay let's start.
Syntax
PHP Code:
<?php //Starts your php script also you can just use <?
?> // Ends your php script
How to say Yo Wuzzzupp
PHP Code:
<?php
echo "Yo Wuzzzupp";
?>
(This will print your text: Yo Wuzzzupp)
Comments
PHP Code:
<?php
/* This is
a comment block */
// This is a comment
# This is also a comment
?>
Variables
PHP Code:
<?php
$myVar = "Hello"; // This is a variable
echo $myVar; // This will print your variable value
?>
(This will print your text: Hello)
If you want to concatenate two or more variables together, use the dot (.) operator
PHP Code:
<?php
$myVar = "Hello";
$myVar2 = "Jaan!";
echo $myVar. " ".$myVar2;
?>
(This will print your text: Hello Jaan!)
If...Else Statements
Syntax
Quote:
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
|
PHP Code:
<?php
$myVar = "Jaan";
if($myVar=="Jaan"){ // If your variable matches that value (Jaan) then Hello Jaan will be shown
echo "Hello Jaan";
}else{ // If that value in your variable is not Jaan then "Hello quest" will be shown
echo "Hello guest!";
}
?>
(This will print your text: Hello Jaan)
PHP Code:
<?php
$day = date("D");
if($day=="Mon"){ // If today is a monday "Have a nice monday!" will be shown
echo "Have a nice monday!";
}else{
echo "Have a nice week!"; // If today is not monday then "Have a nice week!" will be shown
}
?>
Arrays
PHP Code:
<?php
$name = array("Jaan", "Thomas", "Mike");
?>
In this example we assign the ID key manually:
PHP Code:
<?php
$name['0'] = "Jaan";
$name['1'] = "Thomas";
$name['2'] = "Mike";
echo "My name is ".$name['0']. ", my friends name is ".$name['1']." and his friends name is ".$name['2'];
?>
(This will print your text: My name is Jaan, my friends name is Thomas and his friends name is Mike)
Looping
Syntax
Quote:
while (condition)
code to be executed;
|
PHP Code:
<?php
$i = 1;
while($i <=5){ // This will repeats your $i value 5 times
echo "This number is ".$i."<br>";
$i++;
}
?>
Functions
PHP Code:
<?php
function myFunc(){ // This will define your function name
echo "My name is Jaan"; // Inside of { and } you can put everything that you want
}
echo myFunc(); // This will print your funtion
?>
(This will print your text: My name is Jaan)
I hope it helped. Have fun!
