0% found this document useful (0 votes)
281 views16 pages

PHP Revision Note Sinhala

The document provides a comprehensive guide on creating dynamic web pages using PHP and MySQL, covering PHP syntax, data types, operators, conditional statements, loops, and MySQL database connections. It includes numerous examples to illustrate the concepts, such as creating forms, handling user input, and establishing database connections. Additionally, it explains how to create databases and tables using MySQLi in both object-oriented and procedural styles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
281 views16 pages

PHP Revision Note Sinhala

The document provides a comprehensive guide on creating dynamic web pages using PHP and MySQL, covering PHP syntax, data types, operators, conditional statements, loops, and MySQL database connections. It includes numerous examples to illustrate the concepts, such as creating forms, handling user input, and establishing database connections. Additionally, it explains how to create databases and tables using MySQLi in both object-oriented and procedural styles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

10.

7 PHP සහ MySQL භාවිතයෙන් ගතික යෙබ් පිටු නිර්මාණෙ


 PHP යෙස දැක්යෙන්යන් Hypertext Preprocessor ෙන්නයි
මූලික PHP ොග් රීතිෙ(Syntax)
<?php
// PHP code goes here
?>
උදාහරණෙ
<!DOCTYPE html>
<html><body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body></html>
PHP විෙරණ
උදාහරණෙ
<?php
// This is a single-line comment
# This is also a single-line comment
?>
උදාහරණෙ
යේළි කිහිපෙකින් යුත් විෙරණ සඳහා
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
PHP විචෙය
උදාහරණෙ
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
විචෙයෙන් හි ගගෙෙන් ්‍රතිදානෙ
උදාහරණෙ උදාහරණෙ උදාහරණෙ

<?php <?php <?php


$txt = "PHP"; $txt = "PHP"; $x = 5;
echo "I like $txt. "; echo "I like " . $txt . "."; $y = 4;
echo "<br>"; ?> echo $x + $y;
echo " $txt is very interesting"; ?>
?>

PHP හි දත්ත ෙගම


 String
 Integer
 Float (floating point numbers - also called double)
 Boolean
 Array

PHP String

උදාහරණෙ
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>

PHP Integer

-2,147,483,648 හා 2,147,483,647. පරාසයේ පූණම සංඛ්‍යා


උදාහරණෙ
<?php
$x = 5985;
var_dump($x);
?>

PHP Float

දශර්ස්ථාන සහිත සංඛ්‍යා


උදාහරණෙ
<?php
$x = 10.365;
var_dump($x);
?>

PHP Boolean

TRUE යහෝ FALSE ගගෙෙන්


$x = true;
$y = false;

PHP Array

ආරෙක් තුළ ගගෙෙන් කිහිපෙක් ගන්තගමතෙ


උදාහරණෙ
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>

PHP Operators

 Arithmetic operators
 Assignment operators
 Comparison operators
 Increment/Decrement operators
 Logical operators
 String operators
 Array operators
 Conditional assignment operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations,
such as addition, subtraction, multiplication etc.

Operator Name Example


+ Addition $x + $y
- Subtraction $x - $y
* Multiplication $x * $y
/ Division $x / $y
% Modulus $x % $y
** Exponentiation $x ** $y

උදාහරණෙ 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

Assignment 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

උදාහරණෙ 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;
?>

PHP Comparison Operators

Operator Name Example Result


== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y,
and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to
$y, or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or $x >= $y Returns true if $x is greater than or
equal to equal to $y
<= Less than or $x <= $y Returns true if $x is less than or
equal to equal to $y
උදාහරණෙ 1
<?php
$x = 100;
$y = "100";
var_dump($x == $y); // returns true because values are equal
var_dump($x === $y); // returns false because types are not equal
var_dump($x != $y); // returns false because values are equal
var_dump($x <> $y); // returns false because values are equal
var_dump($x !== $y); // returns true because types are not equal
?>

උදාහරණෙ 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
?>

PHP Increment / Decrement Operators

Operator Name Description


++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one

උදාහරණෙ 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--;
?>

PHP Logical Operators


Operator Name Example Result
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true

උදාහරණෙ 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!"; }
} ?>
?>

PHP Conditional Statements

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

The PHP while Loop

උදාහරණෙ
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>

The PHP do...while Loop

උදාහරණෙ 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 Loops

උදාහරණෙ
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>

The PHP foreach Loop

Arrays සර්ග යොදා ගනී

Example
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>
ගභයාස
සංඛ්‍යා යදකක තකතුෙ යසම ර් සඳහා php යක්තෙක් ලිෙන්න

Page 1

totaloftwo.php

<html>
<head>
<title>PHP and Forms Example</title>
</head>
<body>

Add two numbers <br><br>

<form method ="GET" action="totaloutput.php">

<label for = "num1">Enter Number1: </label>


<input type="text" name="num1"><br><br>
<label for ="num2">Enter Number2: </label>
<input type="text" name="num2"><br> <br>

<button type="submit" value="submit" > Add </button>

</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>

<a href="totaloftwo.php">Go Back</a>


</body>
</html>
PHP MySQL Database

MySQL සඳහා සම්බන්ධෙ සැකසීර්

Example (MySQLi Object-Oriented)


<?php
$servername = "localhost";
$username = "root";
$password = "";

// 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);
දත්ත පාදකෙක් සැකසීර්

Example (MySQLi Object-oriented)


<?php

// 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);
?>

දත්ත ෙගුෙක් සැකසීර්

Example (MySQLi Object-oriented)


<?php
// sql to create table
$sql = "CREATE TABLE students (
st_id INT(6) PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50))";

if ($conn->query($sql) === TRUE) {


echo "Table students created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>

Example (MySQLi Procedural)


<?php

// sql to create table


$sql = "CREATE TABLE students (
st_id INT(6) PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50))";

if (mysqli_query($conn, $sql)) {
echo "Table students created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

දත්ත ෙගුෙකට දත්ත ආදානෙ

Example (MySQLi Object-oriented)


<?php

$sql = "INSERT INTO students (st_id,firstname, lastname, email)


VALUES (100,'Sahan', 'Kumara', 'sahankumara@gmail.com')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
Example (MySQLi Procedural)
<?php

$sql = "INSERT INTO students (st_id,firstname, lastname, email)


VALUES (101,'Nimal', 'Perera', 'nimalperera@gmail.com')";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>

දත්ත ෙගුෙක දත්ත ්‍රතිදානෙ


Example (MySQLi Object-oriented)
<?php
$sql = "SELECT* FROM students";
$result = $conn->query($sql);

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

පිෙෙර 3 - පහත පරිදි HTML යපාරර්ෙ සකසන්න

යර්ර් යපාරර්ෙ සර්ග දත්ත පාදකෙ සම්බන්ධ කර Students දත්ත ආදානෙ කිරීර්ට හා ෙගුයේ දත්ත ්‍රතිදානෙ
සඳහා පහත යක්තෙ භාවිතා යේ.

<html lang="en">
<head>
<meta charset="utf-8">
<title>Insert data of students</title>
</head>
<body>

<?php

// Step 1 - First create the connection to the database

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;
}

//step 2 - Joining the form with the table

$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);
}

//step 3 - getting the output to a table

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>";
}

?>

<h3> Insert data of students</h3>

<form action="students.php" method="post">

Number: <input type="text" name="st_id"><br><br>


Name: <input type="text" name="st_name" size = "30"><br><br>
Gender: <input type= "radio" name="gender" value="Male" checked> Male
<input type="radio" name="gender" value="Female"> Female <br><br>
Section: <select name="section">
<option value="OL">OL</option>
<option value="AL">AL</option>
<option value="6-9">6-9</option>
<option value="1-5">1-5</option>
</select>
<br><br><br>
<input type="submit" value="Insert Data"><br><br>
</form>

<?php
listRecord($conn);
$conn->close();
?>

</body>
</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy