Example For See Length & Data Type
Input:<?php
$a = 20;
var_dump($a);
?>
Output: int(20)
Input:
<?php
$a = 20.2;
var_dump($a);
?>
Output: float(20.2)
Input:
<?php
$a = true;
var_dump($a);
?>
Output: bool(true)
Input:
<?php
$a = array("Animash", "Dulal", "Jit", "Shriful"); // Define an Array & put Data
var_dump($a);
?>
Output:
array(4)
[0]=> string(7) "Animash"
[1]=> string(5) "Dulal"
[2]=> string(3) "Jit"
[3]=> string(7) "Shriful"
Example of Basic Strings
Input:<?php
$a = "Your sentence";
echo strlen($a); // See Your Sentence Length
?>
Output: 11
Input:
<?php
$a = "Your sentence";
echo str_word_count($a); // Count your words
?>
Output: 2
Input:
<?php
$a = "Your sentence";
echo strrev($a); // See in Reverse
?>
Output: ecnetnes ruoY
Input:
<?php
$a = "Your sentence is here";
echo strpos($a, "is"); // See Position
?>
Output: 14
Input:
<?php
$a = "Your sentence is here";
echo str_replace("Your", "My", $a); // Replace word
?>
Output: My sentence is here
Example of Constants
Input:<?php
define("value", "My sentence is here"); // Here "value" is a case-sensitive
echo value;
?>
Output: My sentence is here
Input:
<?php
define("value", "My sentence is here"); // Print by Function
function PHP(){
echo value;
}
PHP();
?>
Output: My sentence is here
Input:
<?php
define("value", "My sentence is here"); // Print by Function & return type
function PHP(){
return value;
}
echo PHP();
?>
Output: My sentence is here
Example of Array
Input:<?php
$x = array(
"Animash",
"Pushpa"
);
$y = array(
"Dulal",
"Jit"
);
var_dump($x);
var_dump($y);
?>
Output:
array(2) {
[0]=> string(7) "Animash"
[1]=> string(6) "Pushpa" }
array(2) {
[0]=> string(5) "Dulal"
[1]=> string(3) "Jit" }
Input:
<?php
$x = array(
"a" => "Animash",
"b" => "Pushpa"
);
$y = array(
"c" => "Dulal",
"d" => "Jit"
);
var_dump($x <> $y);
?>
Output: bool(true)
Input:
<?php
$x = array(
"a" => "Animash",
"b" => "Pushpa"
);
$y = array(
"c" => "Dulal",
"d" => "Jit"
);
var_dump($x == $y);
?>
Output: bool(false)
Example of foreach loop
Input:<?php
$names = array("Animash", "Palash", "Jit", "Dulal");
foreach($names as $allname){
echo "$allname <br/>";
}
?>
Output:
Animash
Palash
Jit
Dulal
Example of functions
Input:<?php
function name($university){ // 1 parameter passing
echo "$university is a good university.";
}
name("HSTU");
?>
Output: HSTU is a good university.
Input:
<?php
function name($university){ // 3 parameter passing in 3 time
echo "$university is a good university.<br/>";
}
name("HSTU");
name("SASTC");
name("University of Dhaka");
?>
Output:
HSTU is a good university.
SASTC is a good university.
University of Dhaka is a good university.
Input:
<?php
function name($university="My University"){ // 3 parameter passing in 3 time with 1 default parameter
echo "$university is a good university.<br/>";
}
name("HSTU");
name();
name("SASTC");
name("University of Dhaka");
?>
Output:
HSTU is a good university.
My University is a good university.
SASTC is a good university.
University of Dhaka is a good university.
Input:
<?php
function name($university, $year){ // 2 parameter passing
echo "$university is a good university started $year.<br/>";
}
name("HSTU", "1900");
name("SASTC", "2000");
name("University of Dhaka","1900");
?>
Output:
HSTU is a good university started 1900.
SASTC is a good university started 2000.
University of Dhaka is a good university started 1900.
Input:
<?php
echo "5+10 = ",value(5,10); // 2 value passing as parameter
function value($a, $b){
$c = $a + $b;
return $c;
}
?>
Output: 5+10 = 15
Example of global variable
Input:<?php
$a = 10;
function test(){
global $a;
echo "Access global variable ".$a;
}
test();
?>
Output: Access global variable 10
Example of Super global variables
Input:<?php
$a = 10;
$b = 5;
function test(){ // Super global variable
$GLOBALS['c'] = $GLOBALS['a']+$GLOBALS['b'];
}
test();
echo $c;
?>
Output: 15
Input:
<?php // See your file directory
echo $_SERVER['PHP_SELF'];
?>
Output: /ani/index.php
Input:
<?php // See your server name
echo $_SERVER['SERVER_NAME'];
?>
Output: localhost
Input:
<?php // See your script name
echo $_SERVER['SCRIPT_NAME'];
?>
Output: /ani/index.php
Input:
<?php // See your browser name
echo $_SERVER['HTTP_USER_AGENT'];
?>
Output: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0
Input:
<?php // See your server address
echo $_SERVER['SERVER_ADDR'];
?>
Output: ::1
Example of Using POST Method
Input:<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="username"/>
<input type="submit" name="submit"/> // Create a form
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$name = $_REQUEST['username'];
if(empty($name)){
echo "<span style='color: red'>Username field must not be empty!!</span>";
}
else{
echo "<span style='color: green'>You have submitted: ".$name."</span>";
}
}
?>
Output:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<table>
<tr>
<td>Name </td>
<td><input type="text" name="name"required /></td>
</tr>
<tr>
<td>Email </td>
<td><input type="text" name="email"required /></td>
</tr>
<tr>
<td>Website </td>
<td><input type="text" name="website"required /></td>
</tr>
<tr>
<td>Comment </td>
<td><textarea name="comment"rows="5" cols="40"></textarea></td>
</tr>
<tr>
<td>Gender </td>
<td>
<input type="radio" name="gender" value="female"/>Female
<input type="radio" name="gender" value="male"/>Male
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value = Submit></td>
</tr>
</table>
</form>
<?php
$name =$email =$website =$comment =$gender ="";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$name = validate($_POST["name"]);
$email = validate($_POST["email"]);
$website = validate($_POST["website"]);
$comment = $_POST["comment"];
$gender = $_POST["gender"];
echo "Name: ".$name."<br/>";
echo "Email: ".$email."<br/>";
echo "Website: ".$website."<br/>";
echo "Comment: ".$comment."<br/>";
echo "Gender: ".$gender;
}
function validate($data){
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Output:
Example of Basic Object Oriented php
Input:<?php
class student{ //class
function depertment(){ //Method
return "Animash";
}
function details(){
echo $this->depertment();
}
}
$st = new student; // Object
$st ->details(); // Print
?>
Output: Animash


No comments:
Post a Comment