You are on page 1of 13

PHP Basics

University of Gondar
Faculty of Informatics
Computer Science Department

Internet Proramming

6/9/2019 JavaScript 1
PHP == ‘Hypertext Preprocessor’
What
Open-source, server-side scripting language
is
Used to generate dynamic web-pages
PHP
Executed on the server side (Web Server—Apache)

Various built-in functions allow for fast development

Compatible with many popular databases

6/9/2019 JavaScript 2
What does PHP code looks like
Structurally similar to C/C++

Supports procedural and object-oriented paradigm

All PHP statements end with a semi-colon

Each PHP script must be enclosed in the reserved PHP tag

<?php …?>

6/9/2019 JavaScript 3
Comment in PHP
// Single line comment
# single line comment
/* …… */ multi Line Comment
Variable in PHP
PHP variables must begin with a “$” sign
Case-sensitive ($Foo != $foo != $fOo)
Global and locally-scoped variables
Global variables can be used anywhere
Local variables restricted to a function or class

6/9/2019 JavaScript 4
Output Function of PHP
3 output function in php

Echo, print and die

All the same with slight programming difference

Echo and print are same except print accept only one parameter

Die is like echo except , die will stop the further execution

6/9/2019 JavaScript 5
Echo
• <?php
• Echo “welcome”;
• Echo “php”;
• ?>

Output: welcome php

die
• <?php
• Die “welcome”
• Die “php”
• ?>

Output:welcome

6/9/2019 JavaScript 6
Basics of PHP

Variable declaration use $, E. $name,$age

PHP is loosely typed language

PHP is a case sensitive Scripting Language

(.) is a concatenation operator


6/9/2019 JavaScript 7
Variable naming rule
Variable A variable name must start with letter or underscore

Naming Contain alpha-numeric char and underscore (a-z,A-Z,0-


9,’_’)
Variable name should not contain space

Variable name should not be a PHP keyword (echo, die)

Variable name should be descriptive

6/9/2019 JavaScript 8
Conditional and Loop Statement
Conditional
• If-Else
• Switch-Case
Loop
• For
• While
• Do-while
Operators
• +,*,-,/,%

6/9/2019 JavaScript 9
Insert One PHP file into Another

Insert the content of one PHP file into another

Can be done with include and require

The Two functions are identical except how they handle


errors
• Include() generate warning and continue the execution
• Require() generate fatal error and stop execution

6/9/2019 JavaScript 10
Predefined function PHP with MySQL
Mysqli_connect()->Is a function used to connect to the database server
<?php
$databaseHost = 'localhost';
$databaseName = 'test';
$databaseUsername = 'root';
$databasePassword = '';
$con = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
if (!$con)
{
Die(“connection error:”.mysqli_connect_error());
}
?>

6/9/2019 JavaScript 11
Predefined function PHP with MySQL
Mysqli_query()
• Function used to execute query
• $sql=“SELECT * FROM student”;
• $res=mysqli_query($con,$sql)
Mysqli_fetch_array
• Used to retrieve all recored from the query
• While($re=mysqli_fetch_array($res)){
• Echo $re[‘id’];
• Echo $re[‘name’];
•}

6/9/2019 JavaScript 12
Thank You

6/9/2019 JavaScript 13

You might also like