You are on page 1of 15

Bangladesh Army University of Engineering and Technology (BAUET)

Qadirabad Cantonment, Natore.

Department of Information and Communication Engineering (ICE)

ICE 4261: Internet and Web Programming


Chapter: Web Programming
Lecture-4

Prepared By
Mehedi Hasan Imran
Lecturer, BAUET
Dept. of ICE
Email: imran02@bauet.ac.bd
PHP

The term PHP is an acronym for Hypertext Preprocessor. It is a server-side scripting language that is used for web
development. It can be easily embedded with HTML files. HTML codes can also be written in a PHP file. The PHP
codes are executed on the server-side whereas HTML codes are directly executed on the browser.
Example: Simple program to print “Hello world!” message on the screen.

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 2
Why we learn PHP ?

It is one of the widely used open-source general-purpose scripting language that is used for backend Development.
Apart from this, let’s see why we should learn it.
•Easy to Learn: It is easier to learn for anyone who has come across to any programming language for the first time.
•Free of Cost: Since it is an open-source language, therefore developers are allowed to use its components and all
methods for free.
•Flexible: Since It is a dynamically typed language, therefore there are no hard rules on how to build features using
it.
•Supports nearly all databases: It supports all the widely used databases, including MySQL, ODBC, SQLite etc.
•Secured: It has multiple security levels provides us a secure platform for developing websites as it has multiple
security levels.
•Huge Community Support: It is loved and used by a huge number of developers. The developers share their
knowledge with other people of the community that want to know about it.

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 3
Why use PHP

PHP is a server-side scripting language, which is used to design the dynamic web applications with MySQL database.
•It handles dynamic content, database as well as session tracking for the website.
•You can create sessions in PHP.
•It can access cookies variable and also set cookies.
•It helps to encrypt the data and apply validation.
•PHP supports several protocols such as HTTP, POP3, SNMP, LDAP, IMAP, and many more.
•Using PHP language, you can control the user to access some pages of your website.
•As PHP is easy to install and set up, this is the main reason why PHP is the best language to learn.
•PHP can handle the forms, such as - collect the data from users using forms, save it into the database, and return
useful information to the user. For example - Registration form.

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 4
PHP Features

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 5
PHP Variables

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for PHP variables:

A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two different variables)

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 6
PHP | Types of Errors

Error is the fault or mistake in a program. It can be several types. Error can occur due to wrong syntax or wrong
logic. It is a type of mistakes or condition of having incorrect knowledge of the code.
There are various types of errors in PHP but it contains basically four main type of errors.

Parse error or Syntax Error: It is the type of error done by the programmer in the source code of the program. The
syntax error is caught by the compiler. After fixing the syntax error the compiler compile the code and execute it.
Parse errors can be caused dues to unclosed quotes, missing or Extra parentheses, Unclosed braces, Missing
semicolon etc
<?php
$x = “BAUET";
y = “Information and Communication Engineering";
echo $x;
echo $y;
?>

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 7
PHP | Types of Errors

Fatal Error: It is the type of error where PHP compiler understand the PHP code but it recognizes an undeclared
function. This means that function is called without the definition of function.
<?php

function add($x, $y)


{
$sum = $x + $y;
echo "sum = " . $sum;
}
$x = 0;
$y = 20;
add($x, $y);

diff($x, $y);
?>

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 8
PHP | Types of Errors

Warning Errors : The main reason of warning errors are including a missing file. This means that the PHP function
call the missing file.

<?php

$x = “Department of ICE";

include ("gfg.php");

echo $x . “Information Technology portal";


?>

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 9
PHP | Types of Errors
Notice Error: It is similar to warning error. It means that the program contains something wrong but it allows the
execution of script.

<?php

$x = “ICE";

echo $x; PHP Notice: Undefined variable: new in


/home/84c47fe936e1068b69fb834508d59689.php on line 5
echo $new;
?>

Output:
Explanation: This program use undeclared variable $new so it gives error message.
ICE

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 10
Table of Number

A table of a number can be printed using a loop in program.


Logic:
•Define the number.
•Run for loop.
•Multiply the number with for loop output.
Example:
We'll print the table of 7.

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 11
Some Problems

1. Factorial Program.
2. Armstrong Number.
3. Palindrome Number.
4. Fibonacci Series.
5. Reverse number.
6. Reverse String.
7. Swapping two numbers.
8. Star Triangle.
9. Number Triangle.
10. Alphabet Triangle Pattern.

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 12
PHP MySQL Connect Example

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 13
PHP is a Loosely Typed Language

In the example above, notice that we did not have to tell PHP which data type the variable is.

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a
strict sense, you can do things like adding a string to an integer without causing an error.

In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a
function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.

You will learn more about strict and non-strict requirements, and data type declarations in the PHP Functions chapter.

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 14
Thank You All

03/25/2023 Mehedi Hasan Imran, Lecturer, Department of Information and Communication Engineering 15

You might also like