PHP Revision Note Sinhala
PHP Revision Note Sinhala
PHP String
උදාහරණෙ
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Integer
PHP Float
PHP Boolean
PHP Array
PHP Operators
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations,
such as addition, subtraction, multiplication etc.
උදාහරණෙ 1 උදාහරණෙ 2
<?php <?php
$x = 10; var_dump(pow(2, 8));
$y = 6; echo "<br>";
echo $x + $y; echo(pow(10, 6));
echo “<br>”; ?>
echo $x - $y;
echo “<br>”;
echo $x * $y;
echo “<br>”;
echo $x / $y;
echo “<br>”;
echo $x % $y;
echo “<br>”;
?>
PHP Assignment Operators
උදාහරණෙ 1 උදාහරණෙ 2
<?php <?php
$x = 10; $x = 20;
echo $x; $x += 100;
echo $x;
?>
උදාහරණෙ 3 උදාහරණෙ 4
<?php
<?php $x = 10;
$x = 50; $x *= 3;
$x -= 30; echo $x;
echo $x; ?>
උදාහරණෙ 5 උදාහරණෙ 6
<?php
$x = 10; <?php
$x /= 5; $x = 15;
echo $x; $x %= 4;
?> echo $x;
?>
උදාහරණෙ 2
<?php
$x = 100;
$y = 50;
var_dump($x > $y); // returns true because $x is greater than $y
var_dump($x < $y); // returns true because $x is less than $y
var_dump($x >= $y); // returns true because $x is greater than or equal to $y
var_dump($x <= $y); // returns true because $x is less than or equal to $y
?>
උදාහරණෙ 1 උදාහරණෙ 2
<?php <?php
$x = 10; $x = 10;
echo ++$x; echo $x++;
?> echo "<br>";
echo $x++;
?>
උදාහරණෙ 3 උදාහරණෙ 4
<?php <?php
$x = 10; $x = 10;
echo --$x; echo $x--;
?> echo "<br>";
echo $x--;
?>
උදාහරණෙ 1 උදාහරණෙ 2
<?php <?php
$x = 100; $x = 100;
$y = 50; $y = 50;
if ($x == 100 and $y == 50) { if ($x == 100 or $y == 80) {
echo "Hello world!"; echo "Hello world!";
} }
?> ?>
උදාහරණෙ 3 උදාහරණෙ 4
<?php <?php
$x = 100; $x = 100;
$y = 50; $y = 50;
if ($x == 100 xor $y == 80) { if ($x == 100 && $y == 50) {
echo "Hello world!"; echo "Hello world!";
} }
?> ?>
උදාහරණෙ 5 උදාහරණෙ 6
<?php <?php
$x = 100; $x = 100;
$y = 50; if ($x !== 90) {
if ($x == 100 || $y == 80) { echo "Hello world!";
echo "Hello world!"; }
} ?>
?>
if ්රකාශනෙ
උදාහරණෙ
<?php
$mark = 75;
if ($mark > 50) {
echo "good performance";
}
?>
if...else ්රකාශනෙ
උදාහරණෙ
<?php
$mark =45;
if ($mark > 50) {
echo "good performance";
} else {
echo " improvement required";
}
?>
if...elseif...else ්රකාශනෙ
උදාහරණෙ
<?php
$mark = 68;
if ($mark >= 75) {
echo "Distinction pass";
} elseif ($mark >= 65) {
echo " Merit pass";
} else {
echo "Ordinary pass";
}
?>
switch ්රකාශනෙ
උදාහරණෙ
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
PHP Loops
උදාහරණෙ
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
උදාහරණෙ 1
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
උදාහරණෙ 2
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
උදාහරණෙ
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Example
<?php
$colors = array("red", "green", "blue", "yellow");
Page 1
totaloftwo.php
<html>
<head>
<title>PHP and Forms Example</title>
</head>
<body>
</form>
</body></html>
Page 2
totaloutput.php
<Html>
<head>
<title> addition </title>
</head>
<body>
<?php
$var = $_GET["num1"]+ $_GET["num2"];
echo "Total Is : ".$var;
?>
<br>
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Example (MySQLi Procedural)
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
MySQLi Object-Oriented:
$conn->close();
MySQLi Procedural:
mysqli_close($conn);
දත්ත පාදකෙක් සැකසීර්
// Create database
$sql = "CREATE DATABASE testdb";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
Example (MySQLi Procedural)
<?php
// Create database
$sql = "CREATE DATABASE testdb";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
if (mysqli_query($conn, $sql)) {
echo "Table students created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
$conn->close();
?>
Example (MySQLi Procedural)
<?php
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "Student ID: " . $row["st_id"].
" Name: " . $row["firstname"]. " " . $row["lastname"] . " Email " . $row["email"].
"<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Example (MySQLi Procedural)
<?php
$sql = "SELECT* FROM students";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result))
{
echo "Student ID: " . $row["st_id"].
" Name: " . $row["firstname"]. " " . $row["lastname"] . " Email " . $row["email"].
"<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
HTML යපාරර්ෙක් ර්ගින් දත්ත ෙගුෙකට දත්ත ගදානෙ
පිෙෙර 1 – MySQL ර්ගින් දත්ත පාදකෙ සාදන්න
උදා. mystudents
පිෙෙර 2- mystudents දත්ත පාදකෙ තුළ students ෙගුෙ පහත පරිදි සාදන්න
st_id st_name gender section
යර්ර් යපාරර්ෙ සර්ග දත්ත පාදකෙ සම්බන්ධ කර Students දත්ත ආදානෙ කිරීර්ට හා ෙගුයේ දත්ත ්රතිදානෙ
සඳහා පහත යක්තෙ භාවිතා යේ.
<html lang="en">
<head>
<meta charset="utf-8">
<title>Insert data of students</title>
</head>
<body>
<?php
function connectDB()
{
$server ="localhost";
$username="root";
$password="";
$dbname ="mystudents";
$conn = new mysqli($server,$username,$password,$dbname);
if ($conn->connect_error)
{
die("Database connection failed :".$conn->connect_error);
}
return $conn;
}
$student_no ="";
$studnet_name ="";
$st_gender ="";
$st_section ="";
$conn=connectDB();
function readFormData(){
global $student_no,$studnet_name,$st_gender,$st_section;
$student_no = $_POST["st_id"];
$studnet_name = $_POST["st_name"];
$st_gender = $_POST["gender"];
$st_section = $_POST["section"];
}
function addRecord($conn){
global $student_no,$studnet_name,$st_gender,$st_section;
readFormData();
$sql ="insert into students(st_id,st_name,gender,section)
values('$student_no','$studnet_name','$st_gender','$st_section');";
if(mysqli_query($conn,$sql)){
echo "Data inserted to the Table successfully";
}else {
echo "Try inserting a new record";
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
addRecord($conn);
}
function listRecord($conn){
$sql ="select * from students;";
echo "<table border = 2> <tr><th>Student Number</th><th>Student
Name</th><th>Gender</th><th>Section</th></tr>";
$result = mysqli_query($conn,$sql);
if(!$result){
die("Error in executing the SQL" . $con->error);
}
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>",$row['st_id'],"</td>";
echo "<td>",$row['st_name'],"</td>";
echo "<td>",$row['gender'],"</td>";
echo "<td>",$row['section'],"</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<?php
listRecord($conn);
$conn->close();
?>
</body>
</html>