Basics

I. PHP file

  • PHP files have the extension “.php”.
  • PHP can be embedded directly within HTML ()
  • They can contain text, HTML, CSS, JavaScript, and PHP code.
  • PHP code is executed on the server, and the result is returned to the browser as plain HTML.
<h1>Calculation</h1>
<p>
<?php
echo "Hi there.\n";
$answer = 6 * 7;
echo "The answer is $answer.";
?>
</p>
<p>Another paragraph.</p>

II. How does PHP work?

Here is how PHP works

  • User makes a request.
  • Server processes that request and use a PHP interpreter.
  • Query database (MySQL, MongoDB) and load other files if needed.
  • Result is sent back to the browser/ client. It can send plain text, HTML, pdf, etc.

III. PHP Installation

To install PHP, check official documentation.

If you’re on Mac, you can install PHP with Homebrew.

brew install php

Run this command to ensure you have PHP installed:

brew list | grep php

You’ll need to manually add an alias as follows:

alias php='/usr/local/Cellar/php/8.0.9/bin/php'

Then check for version.

php -v

IV. Syntax

A PHP script can be placed anywhere in the document. It starts with :

<?php
// PHP code
?>

The statement ends with a semi colon ;. If we miss the semi-colon, we’ll get a parse error.

<?php
echo "Hello World";
?>

In PHP, keywords (echo, if, else, etc.), classes, and functions are not case-sensitive.

<?php
ECHO "Hello<br>";
echo "Hello<br>";
EcHo "Hello<br>";
?>

V. Comments in PHP

  • Syntax for single-line comment:
<?php
// This is a single-line comment

# This is also a single-line comment
?>
  • Syntax for multiple-line comments:
/*
This is a
multiple-lines
comment
*/

Note: We should not nest multi-line comments because it results in errors.

VI. Output

There are two ways to get output in PHP: echo and print.

The differences:

  • echo has no return value, while print has a return value of 1.
  • echo can take multiple parameters, while print can take one argument.
  • echo is faster than print.

1. echo

echo is a language construct. It can be treated like a function with one parameter.

Without parentheses, it accepts multiple parameters.

<?php
echo "Hello world!";

2. print

print is a function. It only has one parameter, but parentheses are optional, so it can look like a language construct.

<?php
print "Hello world!";
?>

VII. Strings

  • String literals can use single quotes or double quotes.
  • The backslash \ is used as an “escape” character.
  • Strings can span multiple lines - the newline is part of the string.
  • \n doesn’t work inside of a single-quoted string
  • Concatenation is the “.” not “+”.
  • We can only use variables in double-quote, not single-quote.
<?php
echo "this is a simple string\n";
echo "You can also have embedded newlines in
strings this way as it is
okay to do";

echo "This will expand: \na newline";
// Outputs: This will expand:
// a newline

$expand = 12;
echo "Variables do $expand\n";
// Outputs: Variables do 12

$firstName = "Dev"
echo 'Hello $firstName' // Output: Hello $firstName
echo "Hello $firstName" // Output: Hello Dev

VIII. PHP in HTML

To use PHP in .html file, we can use

  • <?= ?> if we only need to print one thing.
  • <?php ?> if we execute a code block.
<!DOCTYPE html>
<html lang="en">
<body>
    <h1>
        <?php
            $x = 10;
            $y = 5;
            echo $x.", ".$y;
        ?>
        <?= "Hello Word" ?>
    </h1>
</body>
</html>