PHP 5 Notes
PHP 5 Notes
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
Echo
<!DOCTYPE html>
<html>
<body>
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
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!";
}
?>
Syntax
while (condition is true) { <?php <?php
code to be executed; $x = 1; $x = 1;
} do {
while($x <= 5) { echo "The number is: $x <br>";
do { echo "The number is: $x <br>"; $x++;
code to be executed; $x++; } while ($x <= 5);
} } ?>
while (condition is true); ?>
Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$conn->close();
?>
Create Database
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
CREATE TABLE
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
UPDATE DATA
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
DELETE DATA
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
gapw;rp tpdhf;fs;
1. ,izak; MdJ xU xd;wpide;j tiyaikg;G MFk;. ,izgf;fq;fis tbtikg;gjw;F html, php Mfpa nkhopfs;
ngUk;ghYk; gad;gLj;jg;gLfpd;wd.
a. http Ntz;Liffspy; GET kw;Wk; POST Mfpa Ntz;Liffspw;fpilapyhd %d;W gpujhd NtWghLfis
vOJf.
b. xU ve;jpud;; fofj;jpdhy; mjd; Gjpa mq;fj;jtu;fspd; tpguq;fisg; ngWtjw;fhd xU tiyg; gf;fj;jpd; $W
fPNo jug;gLfpd;wJ
2. fPNo jug;gl;Ls;s html gbtj;jpid tbtikg;gjw;fhd gad;gLj;jg;gl;l FwpaPlhdJ fPNo jug;gl;Ls;sJ mjidg;
G+uzg;gLj;Jf
<html>
……………….
…………………………………………………………….
……………….
<body>
<…………………. style="width:300px;">
<………………………..>Result</……………………..>
<……………………. border="1px" width="100%" >
<…….><th>Book Title</th><th>Topic</th></……………>
<?php
$con = ………………………………….("localhost","root","");
$db=……………………………….. ($con,'lms');
if(…………………………………..)
{
$book_id= $_POST[………………………];
$query="……………………………………………………………………………………..;
$query_run=mysqli_query(……………………….., …………………………..);
while($row=…………………………………….. ($query_run))
{
?>
<tr>
<td><?php………………………………………………?></td>
<td><?php………………………………………………..?></td>
</tr>
<?php
}
}
?>
</table>
</fieldset>
</………………. >
</html>
Keywords: