rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages...

9
Department of Software Engineering Mehran University of Engineering and Technology, Jamshoro Course: SW412 – Web Technologies Instructo r Rabeea Jaffari Practical/Lab No. 06 Date CLOs CLO-3: P5 Signature Assessment Score Topic To become familiar with PHP basics and installation of web server Objectives - To learn installation of Xampp server - To learn basic syntax of PHP program - To learn about variables, comments and operators in PHP Lab Discussion: Theoretical concepts and Procedural steps PHP : PHP is an acronym "PHP Hypertext Preprocessor". It is a server-side scripting language that is used to make web pages dynamic which provide different content depending on context) PHP allows us to interface with other services such as database, e-mail, etc. It allows authenticating users and process HTML form information. The latest version of PHP is 7.1 Advantages of using PHP There are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP is the most preferred amongst them due to the following reasons: free and open source: anyone can run a PHP-enabled server free of charge compatible: supported by most popular web servers simple: lots of built-in functionality; familiar syntax ease of use: PHP code can be embedded inside HTML document and vice versa.

Transcript of rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages...

Page 1: rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP

Department of Software EngineeringMehran University of Engineering and Technology, Jamshoro

Course: SW412 – Web TechnologiesInstructor Rabeea Jaffari Practical/Lab No. 06Date CLOs CLO-3: P5Signature Assessment Score

Topic To become familiar with PHP basics and installation of web serverObjectives - To learn installation of Xampp server

- To learn basic syntax of PHP program- To learn about variables, comments and operators in PHP

Lab Discussion: Theoretical concepts and Procedural steps

PHP : PHP is an acronym "PHP Hypertext Preprocessor". It is a server-side scripting language that is used to make web pages dynamic which provide different content depending on context) PHP allows us to interface with other services such as database, e-mail, etc. It allows authenticating users and process HTML form information. The latest version of PHP is 7.1

Advantages of using PHPThere are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP is the most preferred amongst them due to the following reasons:

free and open source: anyone can run a PHP-enabled server free of charge

compatible: supported by most popular web servers simple: lots of built-in functionality; familiar syntax ease of use: PHP code can be embedded inside HTML document and vice

versa.

Web server: To compile and run any programming language, a compiler or interpreter is needed. Likewise, to run a server-side programming language, a web server is needed. A web server is a software program residing on any machine (most likely a server machine) which processes incoming network requests over HTTP and several other related protocols and returns the results to its clients.

PHP Webserver: Apache Apache HTTP web server is used to read/compile the PHP program and then display the output in a web browser.

Xampp: XAMPP is a free and open-source cross-platform web server solution package developed consisting mainly of the

Page 2: rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP

X: cross platformA: Apache HTTP ServerM: MariaDB database server (for database connectivity)and interpreters for scripts written in the P: PHP and P:Perl programming languages.

Xampp can be downloaded for any OS such a Windows, Linux or Mac OSX from the link below:https://www.apachefriends.org/download.html PHP Basic Syntax:

A block or file of PHP code begins with <?php and ends with ?> All the PHP statements, variables, operators, function declarations, etc.

appear between these endpoints. A PHP code file can be written in any text editor such as sublime, atom,

emacs, notepad++ etc. or in a dedicated PHP IDE such as dreamweaver or PHPstorm etc.

Let’s create a file hello.php which outputs the message “Hello world” when executed. The code is as follows:

Save this file with the .php extension. print is a built-in PHP function which is used to print anything on the

browser. This takes only one argument. echo is also a built-in PHP function which is used to print anything on the

browser and can be used as an alternative to the print function above. This function may take multiple arguments and print them at the screen all at once.

Running PHP File:

To run any PHP code file, first place it inside the htdocs folder which can be found in xampp folder after the installation of your xampp package in the dedicated location.

Launch xampp package by double-clicking on it and start the apache server by pressing the start button.

Page 3: rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP

Once the server has been started, go to your browser, type the name of your php code file (for example: hello.php) located inside your server (htdocs) as given below and press enter.http://localhost/hello.php

PHP Variables

PHP is a loosely typed language (like JavaScript or Python) so the data type of any variable is always decided by the value assigned to it (type is not written explicitly). Variable names always begin with $, on both declaration and usage in PHP. The names are case sensitive, shouldn’t start with numbers and usually contain underscore __ to separate words in it.

PHP Comments:

Page 4: rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP

PHP allows the following comment notations:

PHP Operators: There are a number of PHP operators such as:

Arithmetic operators:

Comparison operators:

Page 5: rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP

Logical operators:

Assignment operators:

Page 6: rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP

Conditional operator (ternary: takes 3 operands)

Amongst these operators, !, ++, -- are unary operators (i.e. they take only one operand) and ?: is the ternary operator taking three operands while all the rest are binary operators.

Operator precedence:

Page 7: rabeeajaffari.files.wordpress.com€¦  · Web viewThere are a number of server-side languages available to create dynamic websites such as: Ruby on Rails, JSP, ASP.NET, etc. PHP

Lab TasksSubmission Date: 07-01-19

Write a PHP code for the following and print their outputs (where applicable) on the browser1. To get the PHP version and configuration information.2. To get the name of the owner of the current PHP script, document root

directory under which the current script is running and the operating system PHP is running on.

3. To swap two variables and print their values before and after swapping.

4. To redirect a user to a different web page (for example: http://sw.muet.edu.pk).

5. To display the following strings:

Tomorrow I’ll learn something new.This is a bad command: del c:\*.*\$.

6. To test whether a number is greater than 30, 20 or 10 using ternary operator and print the result.

7. To return the following components of any url stored in a variable such as:

url: http://sw.muet.edu.pk/faculty/cvs/sample.pdfScheme : httpHost : www.sw.muet.edu.pkPath : /faculty/cvs/sample.pdf

Hint: Use string functions to extract various components from the url

Write PHP code inside HTML for the following:Create a variable $var = 'PHP Lab 6'. Put this variable into the title section, h3 tag and as an anchor text within an HTML document to print its value in all of them i.e. in title, h3 tag and anchor tag text.Hint: Save the file with .php extension and run according to PHP basics in the browser. Use echo function between HTML tags to print the value of PHP variable.

Write HTML code inside PHP to display string, values within a table given below:

Hint: Use HTML table tag inside PHP echo statement.