01.Introduction to PHP, Setup, And Basics
01.Introduction to PHP, Setup, And Basics
Programming
Introduction to PHP, Setup, and Basics
Server-Side Programming (Backend)
- Opposite of client side programming (frontend)
- Handles all logics, calculations, and data processing
- Does not show anything to user, just process data
- It helps us in
- Data Validation
- Processing Efficiency
- Business Process Handling
HTTP Request
Client could ask for data from the server by calling certain protocol, but
in the web, the most used protocol is HTTP (HyperText Transfer
Protocol). When you access a certain website, the browser will send
HTTP Request which will be processed by the server.
To see the result, you could access “localhost” on your web browser.
Now, run your Laragon, turn on Apache, and let’s code in PHP
Hello World in PHP
<?php
/**
* this is a multiline comment
*/
// this is a one line comment
# this is a one line comment (new)
?>
PHP: Data Types and Variables
- PHP is similar to JavaScript in terms of data type
- Data types in PHP:
1. Integer
2. Float (double)
3. String
4. Boolean
5. Array
6. Object
7. NULL
PHP: Data Types and Variables
In PHP, a variable does not need to have a data type (unlike C, C++, or Java)
Rules:
- Must start with dollar ($) sign, followed by the name
- May start with letter or underscore after $, not a number
- Must contain only alphanumeric characters and underscore (a-z, A-Z, 0-9,
_)
- Variables are case sensitive ($age and $AGE are different)
PHP: Data Types and Variables
<?php
$name = "John";
$age = 20;
$length = 10.5;
$width = 5.2;
$area = $length * $width;
Notice we could add string by
echo($name . " is " . $age); using “.” (dot)
// OR
echo("$name is $age"); Or put the variable inside string,
this only works using double
?> quote
PHP: Constants
- Constant is variable that cannot be changed
- Does not need $ sign
- Two ways:
- const keyword
- define() function
PHP: Constants
<?php
// method 1: using const
const PI = 3.14;
PI = 22 / 7; // error!
echo(PI);
<?php
echo((1 <=> 2) . "<br/>");
echo((1 <=> 1) . "<br/>");
echo((1 <=> 0) . "<br/>");
?>
<?php
$x = 10;
$y = 5;
echo("Hello World<br/>");
echo $x * $y . "<br/>";
print("PHP Basics<br/>");
print "PHP Basics<br/>";
?>
PHP: Strings
- Could be single quoted ('John') or double quoted ("John")
- String is an “array” of characters, so we could use array approach in a
string
- To connect one string to another, use (.) dot operator
- example: echo($name . " is " . $age)
- We call this “string concatenation”
- To add special characters such as tab, newline,
1. GET will show the parameter data in the URL (BE CAREFUL)
2. POST will hide the parameter data from being displayed
(RECOMMENDED)
<?php
echo("Welcome, " . $_GET["username"] . "<br/>");
echo("Your email is " . $_GET["email"] . "<br/>");
?>
Everything inside $_GET is the “name” of the
form input!
Notice the URL!
After you click submit, notice the URL and see what happen
1. Accepts a number
2. Send number to server (PHP)
3. Show the square value of the number
E.g.
References
https://www.w3schools.com/php/php_operators.asp
https://www.php.net/