KEMBAR78
Lab 3 (PHP) | PDF | Php | Method (Computer Programming)
0% found this document useful (0 votes)
15 views31 pages

Lab 3 (PHP)

The document is a lab guide for CSE 3100: Web Programming, focusing on PHP as a server-side scripting language. It covers topics such as PHP syntax, variables, control structures, functions, object-oriented programming, and form handling. The document also includes installation instructions for XAMPP and references for further learning.

Uploaded by

MD. Sabith
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)
15 views31 pages

Lab 3 (PHP)

The document is a lab guide for CSE 3100: Web Programming, focusing on PHP as a server-side scripting language. It covers topics such as PHP syntax, variables, control structures, functions, object-oriented programming, and form handling. The document also includes installation instructions for XAMPP and references for further learning.

Uploaded by

MD. Sabith
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/ 31

CSE 3100 : Web Programming

Lab 3 : PHP

Farhan Sadaf Kazi Saeed Alam


Lecturer, Assistant Professor,
Dept of CSE, KUET Dept of CSE, KUET
Email: farhansadaf@cse.kuet.ac.bd Email: saeed.alam@cse.kuet.ac.bd
Scripting Language
• Scripting languages does not require compilation, instead they are
interpreted line by line during runtime.

• They include high-level data types, such as lists, associative arrays


and so on.

28-Jan-24 2
Types of scripting language
• Client-side scripting language
• Can be embedded within HTML, commonly are used to add functionality to a
Web page, such as different menu styles or graphic displays or to serve
dynamic advertisements.
• Affecting the data that the end user sees in a browser window.
• JavaScript
• Server-side scripting language
• Manipulate the data usually in a database, on the server.
• PHP, Python

3
PHP
• PHP is a script language and interpreter that is freely available and
used primarily on Linux Web servers.

• PHP, originally derived from Personal Home Page Tools

• Now stands for PHP: Hypertext Preprocessor, which the PHP FAQ
describes as a "recursive acronym."

4
What is PHP?
• PHP stands for PHP: Hypertext Preprocessor
• PHP is a server-side scripting language,
• PHP scripts are executed on the server
• PHP supports many databases (MySQL, Informix, Oracle, Sybase,
Solid, PostgreSQL, Generic ODBC, etc.)
• PHP is an open source software
• PHP is free to download and use

5
Why PHP?
• PHP runs on different platforms (Windows, Linux, Unix, etc.)
• PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
• PHP is FREE to download from the official PHP resource:
www.php.net
• PHP is easy to learn and runs efficiently on the server side

6
What is a PHP File?
• PHP files can contain text, HTML tags and scripts
• PHP files are returned to the browser as plain HTML
• PHP files have a file extension of ".php", ".php5", or ".phtm

7
How PHP Works?

• request of .php file is sent to a web server, which directs


the request to the PHP interpreter.
• the PHP interpreter processes the page
• delivers a web page to the web server to return to the
browser
8
Installing Web Server and PHP
• We use a package for installing web server and PHP called XAMPP.
• XAMPP includes:
• Apache (IPv6 enabled) + OpenSSL
• MySQL + PBXT engine
• PHP
• phpMyAdmin
• Perl
• FileZilla FTP Server
• Mercury Mail Transport System

9
Basic PHP Syntax
<html>
<body>

<?php
echo "Hello World<br>";
echo date("d-m-y");
// This is a comment

/*
This is
a comment
block
*/
?>

</body>
</html>
10
Variables in PHP
• All variables in PHP start with a $ sign symbol.
$var_name = value;
• Variable naming Rules: *case sensitive
• A variable name must start with a letter or an underscore "_"
• A variable name can only contain alpha-numeric characters and
underscores (a-z, A-Z, 0-9, and _ )
• A variable name should not contain spaces. Words can be separated
with an underscore ($my_string), or with capitalization ($myString)

11
Variables in PHP
• You do not have to tell PHP which data type the variable is.
• PHP has no command for declaring a variable.
• The data type depends on the value of the variable.
• Try assigning a value to a variable and See what var_dump(variable)
returns.
• To create a constant you can use ‘const’ keyword or use define()
function: define(name, value);

12
PHP strings and operators
String example:
<?php
$txt1="Hello World!";
• Concatenate using . (dot) operator
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
• There are various string functions you can try: strlen(), str_word_count(),
strpos(), strtoupper(), strtolower(), str_replace(), strrev(),
trim(), explode(), substr() and so on
Operators:
• Basic all operators can be used in php as you can use in C or C++
• Almost same expression style

13
PHP If...Else Statement
<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
echo "<p>The hour (of the server) is " . $t;
echo ", and will give the following message:</p>";

if ($t < "10") {


echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
</body>

14
PHP Switch Statement
<!DOCTYPE html>
<html>
<body>
<?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!";
}
?>
</body>
</html>

15
PHP Loops For loop example :

<?php
While Loop : for ($x = 0; $x <= 10; $x++) {
while (condition){ if ($x == 3)
the code goes here… break;
} echo "The number is: $x <br>";
}
?>
While Loop (alternative) :
while (condition)
the code goes here…
For each loop example : loop through the items of an array.
endwhile;
<?php
$colors = array("red", "green", "blue", "yellow");
Do while loop: foreach ($colors as $x) {
Do { echo "$x <br>";
the code goes here… }
} while (condition); ?>

16
PHP Functions
• PHP has a lot of built-in functions, and in addition you can create your own
custom functions.
• The declaration starts with the keyword function.
• A function name must start with a letter or an underscore.
• Function names are NOT case-sensitive.
Declaration ->
function functionName(arguments…) {
statements goes here…
}
Calling ->
functionName(arguments…);

• Same like c/c++, you can also pass arguments by Reference [TRY]

17
PHP Variadic Functions
• By using the ... operator in front of the function parameter, the function accepts an
unknown number of arguments.
• This is also called a variadic function.
• A function that do not know how many arguments it will get.
• You can only have one argument with variable length, and it has to be the last
argument.
function myFamily($lastname, ...$firstname) {
txt = "";
$len = count($firstname); $a = myFamily("Doe", "Jane",
for($i = 0; $i < $len; $i++) { "John", "Joey");
$txt = $txt."Hi, $firstname[$i] $lastname.<br>"; echo $a;
}
return $txt;
}

18
PHP Arrays
• Arrays in php can be indexed, associative or multidimensional.

• Indexed -> $cars = array("Volvo", "BMW", "Toyota");

• Associative -> $car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);

• To access an array item in associative array you can refer to the key name.
echo $car["model"]

• Other functionalities are same like c/c++

19
PHP OOP - Classes and Objects
class Fruit {
// Properties
• Class definition public $name;
public $color;
<?php
class ClassName { // Methods
function set_name($name) {
//code goes here… $this->name = $name;
}
} function get_name() {
• Class can have attributes and methods. }
return $this->name;

• You can assign access modifiers to the }


$apple = new Fruit();
attributes. $banana = new Fruit();
• $this keyword refers to the current object, $apple->set_name('Apple');
$banana->set_name('Banana');
and is only available inside methods.
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();

20
PHP OOP – Constructor/Destructor
• __construct() function is automatically called when you create an object from a class.
• __destruct() function is automatically called at the end of the script.

function __construct($name) {
$this->name = $name;
}

function __destruct() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}

21
PHP OOP – Inheritance
• An inherited class is defined by using the extends keyword.
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

// Strawberry is inherited from Fruit


class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
22
PHP OOP – Inheritance
• Inherited methods can be overridden by redefining the methods

• The final keyword can be used to prevent class inheritance or to


prevent method overriding
<?php
final class Fruit {
// some code
}

23
PHP OOP – Abstract Classes
• An abstract class is a class that contains at least one abstract method.
• An abstract method is a method that is declared, but not implemented in
the code.
• Abstract classes/methods are defined with the abstract keyword
abstract class ParentClass {
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() : string;
}

24
PHP OOP – Interfaces
• Interfaces allow you to specify what methods a class should implement.
• Interfaces are declared with the interface keyword.
• When one or more classes use the same interface, it is referred to as
"polymorphism".
• A class uses the keyword implements to implement an interface.
interface InterfaceName {
public function someMethod1();
public function someMethod2($name, $color);
public function someMethod3() : string;
}

• Interfaces vs. Abstract Classes???


25
PHP OOP – Traits
• PHP does not support multiple inheritance.
• A child class can inherit only from one single parent.
• Traits can be used to solve it.
• Traits are used to declare methods that can be used in multiple classes.
• Traits can have methods and abstract methods and the methods can have any access
modifier.
• Traits are decalred with the trait keyword.
• To use a trait in a class, use keyword is used.
trait TraitName { class MyClass {
// some code... use TraitName;
} }

26
PHP OOP – Statics
• Static methods can be called directly - without creating an instance of the class first.
• To access a static method use the class name, double colon (::), and the method name.
• Class properties can be static too.

<?php
class greeting {
public static function welcome() {
echo "Hello World!";
}
}

// Call static method


greeting::welcome();
?>

27
PHP OOP – Namespaces
• A namespace is a way to encapsulate items like classes, functions, and constants.
• Better organization and structured code.
• You define a namespace using the namespace keyword. It is typically the first
statement in a PHP file.
• To access classes from outside a namespace, the class needs to have the namespace
attached to it using ‘\’.
• You can also use the namespace keyword at the top
namespace MyNamespace;
class MyClass { $var = new MyNamespace\ MyClass();
// Class definition
} namespace Html;
function myFunction() { $table = new Table();
// Function definition
}
const MY_CONSTANT = 42;
28
PHP OOP – Include/Require
• The include and require statements are used to include and evaluate the content of
another PHP file within the current file.
• The require statement is similar to include but behaves more critically.
• If the specified file is not found, it produces a fatal error and stops the script execution.

<?php <?php
include 'header.php'; require 'config.php';
echo "This is the main content."; // Rest of the code
?> ?>

29
PHP Form Handling
• Task:

• For form handling in php, now open the “Tutorial Lab 03.pdf” file and
start working on it.

30
Reference
• https://www.w3schools.com/

28-Jan-24 31

You might also like