Wednesday, February 3, 2016

PHP for beginners 1

example:
<?php
// Echo

$value = "ProgrammingKnowledge";
echo "Hello=$value <br>";
echo 'Hello='.$value.'<br>';
$googlelink = "Google Link";
echo '<a href ="http://www.google.com">'.$googlelink.'</a><br>';
echo 'it\'s a nice day';

// if statement

$num1 = '10';
$num2 = 10;
if ($num1 == $num2)
  echo "Equal";
else echo "Not equal";

// Array

$names = array('Mark'=>65, 'John'=>25, 'Bob'=>29);
echo 'Weight of Mark is '.$names['Mark'];
print_r($names);

// Multidimensional Arrays in PHP

$students = array(array('Name'=>'Mark', 'Age'=>15, 'Weight'=>56),
array('Name'=>'Jonh', 'Age'=>15, 'Weight'=>56),
array('Name'=>'Bob', 'Age'=>15, 'Weight'=>56));
// echo $students[0][0].' is here : '. $students[0][1] .' and '. $students[0][2];
echo $students[1]['Name'].' is here : '. $students[1]['Age'].' and '.$students[1]['Age'];

// While Do...While

$counter = 1;
while($counter<=10){
echo "ProgrammingKnowledge ".$counter++."<br>";
}
for($counter=10; $counter>0; $counter--)
echo "ProgrammingKnowledge ".$counter."<br>";

// foreach

$names = array('Mark', 'John', 'Tom', 'Partick', 'July');
foreach( $names as $name)
{
echo $name.'<br>';
}
$students = array(array('Name'=>'Mark', 'Age'=>15, 'Weight'=>56),
array('Name'=>'Jonh', 'Age'=>15, 'Weight'=>56),
array('Name'=>'Bob', 'Age'=>15, 'Weight'=>56));
foreach($students as $stud => $inner)
{
echo '<b>'.$stud.'</b> ';
foreach($inner as $item)
{
echo $item.' ';
}
echo '<br>';
}

// switch

$grade = 'C';
switch($grade)
{
case 'A': echo "Excellent"; break;
case 'B': echo "Very good"; break;
case 'C': echo "Good"; break;
case 'D': case 'F': echo "Not Bad"; break;
default: echo "No grades found"; break;
}
?>

GET example:
Tutorial2.php
<?php
// Get
if(isset($_GET["stu_name"]) && isset($_GET["stu_age"]) && isset($_GET["stu_weight"]) ){
            $name = $_GET["stu_name"];
            $age = $_GET["stu_age"];
            $weight = $_GET["stu_weight"];  
            if(!empty($name) && !empty($age) && !empty($weight)){
                                    echo 'Name:'.$_GET["stu_name"].'<br>';
                                    echo 'Age:'.$_GET["stu_age"].'<br>';
                                    echo 'Weight:'.$_GET["stu_weight"].'<br>';        
            }
}  else {
            echo "Please enter all field";
}
?>
Tutorial2_student.php
<html>
<body>
<h1>Student info</h1>
<form action = "tutorial2.php" method ="get">
<b>Name:<b><input type="text" name="stu_name">
<b>Age:<b><input type="text" name="stu_age">
<b>Weight:<b><input type="text" name="stu_weight">
<br>
<input type="submit">
</form>
</body>
</html>
POST example:
Tutorial3.php
<?php
// POST
$_POST["username"];
$_POST["password"];
if($_POST["username"]==="mark" &&  $_POST["password"]==="pass"){
            echo "username and password is correct";
} else echo "username and password is not correct";

?>
Tutorial3_login.php
<html>
<body>
<h1>Login Form</h1>
<form action="tutorial3.php" method="post">
<b>Username:<b><input type="text" name="username">
<b>Password:<b><input type="password" name="password">
<br>
<input type="submit">
</form>
</body>
</html>
Functions with Arguments example:
<?php
function hello_world($num1, $num2){
            echo $num1 + $num2.'<br>';
}
function add($num1, $num2){
            $result = $num1 + $num2;
            return $result;
}
//          hello_world(15, 35);
//                      hello_world(55, 156);
$add1 = add(25, 28);
$add2 = add(55, 12);
echo $add1 * $add2;
?>
Date and Time example:
<?php
// Day/Month/Year
$date = date('d-m-y');
echo $date.'<br>';
// hour:minutes:second
$time = date('h-m-s');
echo $time;
?>
Global Variable example:
<?php
global $name = "Mark";
function showMe(){
            global $name;
            echo '<b>The name of the student is: </b>'.$name;
}
showMe();
?>
include() and require() functions example:
tutorial4.php
<?php
include 'tutorial4_header.php';
include 'tutorial4_header.php';
include_once ('tutorial4_header.php');

echo $page1.'<br>';
echo $page2;
?>
Tutorial4_header.php
<html>
<body>
<h1>Programming Knowledge</h1>
</body>
</html>
<?php
            $page1 = "We are page 1";
            $page2 = "We are page 2";
?>

No comments:

Post a Comment