I made this PHP tutorial as simple as possible for you to grapple the basics of the PHP programming language. You can also use this as a good reference of the basics of PHP. So I suggest you bookmark this post and come back here whenever you need. You are most welcome. (^_^)
1. Basic Syntax
A PHP scripting block always starts with <?php and ends with ?>. It can be placed anywhere in the document.
You can also use the shorthand symbol, <? and end with ?>, on servers with shorthand support enabled
However, it is recommended that you use the standard form (<?php) rather than the shorthand form (<?) for maximum compatibility,
Example:
<?php
echo "Hello World";
?>
2. Comments
Use // sign to make a single-line comment or /* and */ signs to make a large comment block.
Example;
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
3. Variables
In php, variables start with a $ sign symbol. Variables may contain strings, numbers, or arrays but should start with a letter.
Rule:
* A variable name must start with a letter or an underscore "_"
* A variable name can only contain alpha-numeric characters and
underscores (a-Z, 0-9, and _ )
* A variable name should not contain spaces. If a variable name should be more
than one word, it should be separated with underscore ($my_string),
or with capitalization ($myString)
Valid: $a, $name, $name1
Invalid: $3, $3name
Example
<?php
$text="Hello World";
echo $text;
?>
4. Operators
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (division remainder)
++ Increment
-- Decrement
Assignment Operators
Operator Example The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
Comparison Operators
Operator Description
== is equal to
!= is not equal
> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to
Logical Operators
Operator Description
&& and
|| or
! not
5. Conditional Statements
if...statement
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Have a nice weekend!";
}
?>
The If...Else Statement
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Have a nice weekend!";
}
else
{
echo "Have a nice day!";
}
?>
The ElseIf Statement
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
The Switch Statement
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
6. Arrays
Indexed Array
$names = array("Babyrose", "Kabalweg");
or
$names[0] = "Babyrose";
$names[1] = "Kabalweg";
Associative Arrays
$age = array("Babyrose"=>27, "Kabalweg"=>28);
$ages["Babyrose"] = "27";
$ages["Kabalweg"] = "28";
Multidimensional Arrays
$families = array
(
"Badinas"=>array
(
"Joel",
"Kabaleg"
),
"Aranas"=>array
(
"Rosalina",
"Babyrose"
)
);
7. Looping
while loop
<?php
$x=1;
while($x<=10)
{
echo "The number is " . $x . "<br />";
$x++;
}
?>
do...while loop
<?php
$x=1;
do
{
echo "The number is " . $x . "<br />";
$x++;
}
while ($x<10);
?>
for loop
<?php
for ($x=1; $x<=10; $x++)
{
echo "The number is " . $x . "<br />";
}
?>
foreach loop
<?php
$arr=array("one", "two", "three", "four", "five");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</pre>
8. Functions
Simple
<?php
function sayMyName()
{
echo "You are Joel Badinas";
}
?>
Parameterized
<?php
function sayMyName($name)
{
echo "You are ".$name;
}
?>
Returning Values
<?php
function sayMyName()
{
$name = "You are Joel Badinas";
Return $name;
}
?>
0 comments:
Post a Comment