Martin Francis

DSA PHP Introduction

PHP scripting

Introduction

php commands are typically embedded in an HTML page which is saved with a suffix of .php. The webserver Windows IIS or Apache should be configured to pass such files through the PHP interpreter. Output will be returned to the requestor.

This script shows some of the basics of PHP - you need to read the script alongside the output from the script to see how each structure works.

Sample output from this PHP script

PHP has evolved to be a complex language which will take some time and effort to learn in all its glory. This example aims to show you you the most commonly used parts of PHP, and should be enough to read simple scripts.

Handling access to to Form data and linking to dtabases will be in a further worksheet

Sections of php script start with <?php and are terminated by ?>

They can be just snippets : Time is 23:54 . or whole programs

Fred

Printing

222222

String

PHP executes on the server

$s was developed by $t

PHP was developed by Rasmus Lerdorf

PHP was developed by Rasmus Lerdorf

Numbers

Bases 10: 255 16: 255 8: 255

c=1 d=7.2

Logic

FalseFalseFalseFalseTrueTrueTrue

Arrays

Array ( [0] => 1 [5] => 12 [6] => 3 )

Control Structures

A is FOUR

B is not FOUR

1 squared is 1 3 squared is 9 5 squared is 25 7 squared is 49 9 squared is 81

PHP was created by Rasmus Lerdorf

COBOL was created by Grace Hopper

Number functions

  • Square root of 2 : 1.4142135623731
  • Rounded to two places : 1.41
  • a Random number between 1 and 10 : 8

String functions

  • String : PHP provides a large library of functions
  • length is 43
  • first two characters P
  • last three characters ns
  • position of "large" 16
  • uppercase the string PHP PROVIDES A LARGE LIBRARY OF FUNCTIONS
  • lowercase the string php provides a large library of functions
  • trim whitespace from both ends PHP provides a large library of functions
  • base64 encoded IFBIUCBwcm92aWRlcyBhIGxhcmdlIGxpYnJhcnkgb2YgZnVuY3Rpb25zIA==
  • base64 decoded PHP provides a large library of functions

Date functions

  • Current Time : 23:54
  • Day of week: Thursday
  • Date : 28/03/24
  • Date info Array ( [seconds] => 46 [minutes] => 54 [hours] => 23 [mday] => 28 [wday] => 4 [mon] => 3 [year] => 2024 [yday] => 87 [weekday] => Thursday [month] => March [0] => 1711670086 )

Arrays

  • array dump : Array ( [0] => Sunday [1] => Monday [2] => Tuesday [3] => Wednesday [4] => Thursday [5] => Friday [6] => Saturday )
  • length is 7