PHP Language Basics
Learn PHP in 15 minutes
-
Semicolons are mandatory in PHP
-
PHP code can be embedded in HTML pages
1 2 3 4 5 6 7 8 9 10 11 12
<html> <head> <title>Learning PHP</title> </head> <body> <?php echo "Hello World !"; ?> </body> </html>
-
Variables are declared with
$
. For example:$myvar = "This is my variable"
. PHP is not strict with the data types of the variables. -
To concatenate strings use
.
. For example:echo "Hello, " . $name
. -
Creating array :
$people = array("Alice", "Bob", "Cathy")
-
Looping through array
1 2 3 4 5 6
<?php $people = array("Alice", "Bob", "Cathy") foreach ($people as $person) { echo $person . ' '; } ?>
PHP Programming Language Tutorial - Full Course
Hello World
- PHP start development webserver :
php -S localhost:4000
- PHP is tightly coupled with HTML. You can write html code in
.php
file and the file renders like a html file when opened in a browser. - Semicolons are mandatory in PHP
Variables
- To include a variable in echo statement:
1 2 3 4
<?php $characterName = "John"; echo "There once lived a man named $characterName"; ?>
Data Types
- String :
$phrase = "Hello, "
- Integer :
$number = 30
(can be positive or negative) - Decimal (floating numbers) :
$gpa = 3.33
- Boolean :
$loggedIn = true
- Null value :
null
Working with Strings
- Convert string to lowercase :
strtolower()
- Convert string to uppercase :
strtoupper()
- Get string length :
strlen()
- String is an array of characters. The characters can be accessed by the index of a string. Ex:
"Mike"[0]
giveM
- Replacing sub strings :
str_replace("Google", "Alphabet", $companyName);
- Get sub string :
substr($phrase, 8, 3)
where 8 is the index of the starting character and 3 is the length of substring. If the length is not provided, then PHP gives the substring till the end of the phrase.
Working with Numbers
- Basic math operations :
+
,-
,*
,/
,%
- Incrementing :
$num++
- Decrementing :
$num--
- Max of numbers :
max(2, 10)
- Min of numbers :
min(2, 10)
Getting user input
|
|
Use $_POST["name"]
for data sent through POST message
Arrays
- Create array:
$friends = array("asfds", "ddfvs", "rfdfs");
- Array can store any data type
- A new element can be added at any index position of the array. For example, in the above
$friends
array, I can add new friend at index 10 :$friends[10] = "Pot"
- To get the total number of elements in array :
count($friends)
Associative array
- This is like dictionary in Python
- Example:
$grades = array("Jim"=>"A+", "Pam"=>"B-")
- The keys must be unique
- Get the number of elements :
count($grades)
Functions
Function taking arguments
|
|
Function with returns
|
|
If statements
|
|
Switch statements
|
|
While loops
|
|
Do-While loops
|
|
For loops
|
|
Comments
|
|
Including HTML
|
|
In the above example, header.html
and footer.html
are HTML files on the same directory as the php file.
Include PHP
article-header.php
|
|
site.php
|
|
In a similar way, including a php file allows you to use the functions & variables defined in the included file.
Classes & Objects
- Class is a specification of a custom data type
- Object is an instance of a class
|
|
Constructors
- Constructor is the function that gets called whenever we create an object of a class
- Always named
__construct()
|
|
Object functions
|
|
Getters and Setters
- Visibility modifier : defines what different attributes of an object can code access
- When a variable is declared using
var
, its public by default - Getters and setters are special functions written inside a class to get & set the (mostly private) attributes
|
|
Inheritance
|
|