CMPF123 - Chapter 5

Chapter Overview

  • Title: PHP: Hypertext Preprocessor

  • Course: CMPF 123: Introduction to Web Development

Learning Outcomes

  • At the end of this lesson, students should be able to:

    • Understand basic PHP concepts

    • Perform basic operations using PHP

    • Apply PHP codes in HTML pages where appropriate

Topics Covered

  • Introduction to PHP

  • Strings

  • Installation of PHP

  • Constants

  • Syntax

  • Operators

  • Variables

  • Conditional Statements

  • Echo/Print

  • Loops

  • Data Types

PHP Basics

What is PHP?

  • Definition: PHP stands for "PHP: Hypertext Preprocessor".

  • Type: PHP is a server scripting language and a powerful tool for creating dynamic and interactive web pages.

  • Comparison: It is a widely-used, free, efficient alternative to Microsoft ASP.

Understanding PHP Files

  • Knowledge Requirement: Students should have a basic understanding of HTML, CSS, and JavaScript.

  • Content: PHP files can contain text, HTML, CSS, JavaScript, and PHP code.

  • Execution: PHP code is executed on the web server, and results are returned as plain HTML.

  • File Extension: PHP files have the extension ".php".

PHP Functionalities

  • Can:

    • Generate dynamic page content

    • Handle files on the server (create, open, read, write, delete)

    • Collect form data

    • Manage cookies

    • Manipulate database data

    • Control user access

    • Encrypt data

Requirements to Start with PHP

  • Option 1: Find a web host that supports PHP.

  • Option 2: Install a web server on your PC and then PHP.

    • Example Tools:

      • WampServer: A web development platform for Windows.

      • XAMPP: Provides Apache server, MySQL, PHP, and Perl for multiple operating systems.

Creating and Executing PHP Programs

Initial Setup

  1. Open a code editor.

  2. Type the code:

    <?php
    phpinfo();
    ?>
  3. Save the file as phpinfo.php in the web server’s root directory (e.g., wamp/www).

Test the Setup

  1. In your web browser, navigate to: http://localhost/phpinfo.php. If successful, congratulations!

PHP Syntax

  • A PHP script is executed on the server, and the resulting HTML is sent back to the browser.

  • PHP scripts begin with <?php and end with ?>.

Basic PHP Program Example

<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php echo "Testing 123"; ?>
</body>
</html>

PHP Comments

  • Used to:

    • Explain code to others.

    • Remind oneself of code functionality.

Case Sensitivity in PHP

  • Keywords (if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.

  • Variable names are case-sensitive.

PHP Variables

  • Variables start with a $ sign.

  • Examples:

    <?php
    $txt = "Hello world!";
    $x = 5;
    y=10.5;</code></pre></li><liclass="drag"><p><strong>Rules</strong>:</p><ul><liclass="drag"><p>Muststartwithaletterorunderscore.</p></li><liclass="drag"><p>Cannotstartwithanumber.</p></li><liclass="drag"><p>Cancontainalphanumericcharactersandunderscores.</p></li><liclass="drag"><p>Arecase−sensitive.</p></li></ul></li></ul><h3><spanclass="heading−content">Echo/PrintStatements</span></h3><ul><liclass="drag"><p><strong>Echo</strong>:Astatementusedtooutputdata.</p></li><liclass="drag"><p>Exampleusage:</p></li></ul><pre><codeclass="language−php"><?phpecho"Hello,World!";?></code></pre><ul><liclass="drag"><p>Outputtingvariablesandresultsfromoperationscanalsobedoneusingecho.</p></li></ul><h3><spanclass="heading−content">DataTypesinPHP</span></h3><ul><liclass="drag"><p><strong>SupportedTypes</strong>:</p><ul><liclass="drag"><p><strong>String</strong>:Anytextwithinquotes.</p></li><liclass="drag"><p><strong>Integer</strong>:Non−decimalnumbersinspecifiedrange.</p></li><liclass="drag"><p><strong>Float</strong>:Numberswithdecimalsorexponentialform.</p></li><liclass="drag"><p><strong>Boolean</strong>:Representstrueorfalsevalues.</p></li><liclass="drag"><p><strong>Array</strong>:Storesmultiplevaluesinonevariable.</p></li><liclass="drag"><p><strong>Object</strong>:Instancesofclasses.</p></li><liclass="drag"><p><strong>NULL</strong>:Representsavariablewithnovalue.</p></li><liclass="drag"><p><strong>Resource</strong>:Referencetoexternalresources.</p></li></ul></li></ul><h3><spanclass="heading−content">PHPArrays</span></h3><ul><liclass="drag"><p><strong>Definition</strong>:Anarraycanstoremultiplevalues.</p></li><liclass="drag"><p><strong>Example</strong>:</p></li></ul><pre><codeclass="language−php">y = 10.5;</code></pre></li><li class="drag"><p><strong>Rules</strong>:</p><ul><li class="drag"><p>Must start with a letter or underscore.</p></li><li class="drag"><p>Cannot start with a number.</p></li><li class="drag"><p>Can contain alphanumeric characters and underscores.</p></li><li class="drag"><p>Are case-sensitive.</p></li></ul></li></ul><h3><span class="heading-content">Echo/Print Statements</span></h3><ul><li class="drag"><p><strong>Echo</strong>: A statement used to output data.</p></li><li class="drag"><p>Example usage:</p></li></ul><pre><code class="language-php"><?php echo "Hello, World!"; ?></code></pre><ul><li class="drag"><p>Outputting variables and results from operations can also be done using echo.</p></li></ul><h3><span class="heading-content">Data Types in PHP</span></h3><ul><li class="drag"><p><strong>Supported Types</strong>:</p><ul><li class="drag"><p><strong>String</strong>: Any text within quotes.</p></li><li class="drag"><p><strong>Integer</strong>: Non-decimal numbers in specified range.</p></li><li class="drag"><p><strong>Float</strong>: Numbers with decimals or exponential form.</p></li><li class="drag"><p><strong>Boolean</strong>: Represents true or false values.</p></li><li class="drag"><p><strong>Array</strong>: Stores multiple values in one variable.</p></li><li class="drag"><p><strong>Object</strong>: Instances of classes.</p></li><li class="drag"><p><strong>NULL</strong>: Represents a variable with no value.</p></li><li class="drag"><p><strong>Resource</strong>: Reference to external resources.</p></li></ul></li></ul><h3><span class="heading-content">PHP Arrays</span></h3><ul><li class="drag"><p><strong>Definition</strong>: An array can store multiple values.</p></li><li class="drag"><p><strong>Example</strong>:</p></li></ul><pre><code class="language-php">cars = array("Volvo", "BMW", "Toyota");

    PHP Constants

    • Define constant values that cannot change: define(name, value, case-insensitive).

    PHP Operators

    Arithmetic Operators

    Operator

    Description

    Example

    Result

    +

    Addition

    $x + $y

    Sum of $x and $y

    -

    Subtraction

    $x - $y

    Difference of $x and $y

    *

    Multiplication

    $x * $y

    Product of $x and $y

    /

    Division

    $x / $y

    Quotient of $x and $y

    %

    Modulus

    $x % $y

    Remainder of $x / $y

    **

    Exponentiation

    $x ** $y

    $x raised to the power of $y

    Comparison Operators

    Operator

    Description

    Example

    Result

    ==

    Equal

    $x == $y

    True if $x equals $y

    ===

    Identical

    $x === $y

    True if both value and type are equal

    !=

    Not Equal

    $x != $y

    True if $x is not equal to $y

    Conditional Statements

    Types

    • if: Executes code if a condition is true.

    • if...else: Executes different code based on conditions.

    • if...elseif...else: Handles multiple conditional checks.

    • switch: Selects from many blocks of code to be executed based on variable value.

    PHP Loops

    • while: Executes block while condition is true.

    • do while: Executes at least once and continues while condition is true.

    • for: Loops for a specified number of iterations.

    • foreach: Loops through each element of an array.

    Thank You!

    • Expressing gratitude in various languages.

      • Example phrases included: "Gracias", "Arigato", "Merci", etc.