PHP Programming Basics

Image: PHP Programming Basics
Image: PHP Programming Basics

PHP is a server-side programming language. To parse PHP code set up a local environment such as XAMPP to run Apache server PHP on your local machine and use MySQL database. In PHP Programming Basics, embed PHP code into an HTML page and output dynamic text. Review the basics – PHP data types, such as variables, strings, and arrays. Different control structures in PHP, such as expressions and loops, give control over how the code executes. Even though PHP has many built-in functions, you can define your own custom functions. The code becomes reusable and helps prevent bugs.

OOP or object-oriented programming in PHP is the concept of class and objects with properties and methods. The class is associated with the definition and the object with the value. Interact with the database using the OOP code.

Server Side PHP Programming Basics

1 - PHP Programming Basics: XAMPP: for Mac download
1 – PHP Programming Basics: XAMPP: for Mac download

Environment Setup – XAMPP

Install local servers – either XAMPP, | MAMP (Mac) or WAMP (Windows) or LAMP (Linux). After the installation run MySQL.

2 - XAMPP: Control Panel - Manage Servers
2 – XAMPP: Control Panel – Manage Servers

Download XAMPP as shown in fig 1. Click on Manage Servers in the menu bar. Next Start MySQL Database and Apache Web Server fig 2 till the status shows ‘Running’ and the ‘red’ icon turns ‘green’. Access your local server by typing 127.0.0.1 or localhost in the browser.

3.1 - PHP Programming Basics Code
3.1 – PHP Programming Basics Code
3.2 - Output of PHP Programming Basics Code
3.2 – Output of PHP Programming Basics Code

To write PHP codes, we will use Brackets – a free open source code editor fig 3.1 & 3.2. After writing codes in PHP save it as a .php extension and then choose your ‘XAMPP folder’ (located in Applications for a Mac) to save it to ‘htdocs‘ folder in ‘xamppfiles‘.

Variables, Arrays, Loops, and Conditional in PHP Programming Basics.

3.1 - PHP Programming Basics - variablesdotphp
4.1 – PHP Programming Basics – variablesdotphp
3.2 - PHP Programming Basics - Output of variablesdotphp
4.2 – PHP Programming Basics – Output of variablesdotphp

In PHP, the variable starts with the $ sign, followed by the name of the variable. They store values such as numeric, characters, character strings, or memory addresses which are used in the program. Fig 4.1 is the variables.php file where a simple PHP code is written showing variables – string, character, numerical and float and echo is used to output the result fig 3.2 & fig 4.2.

localhost/sapCanvas_demo/variables.php in the browser (fig 3.2 & 4.2) shows:

variables.php – is the name of the program.
sapCanvas_demo – is the folder which saves your program file variables.php  
localhost – is the Xampp directory which you use to access the server.

4.1 - Using print in place of echo
5.1 – Using print in place of echo
4.2 - Output for print in place of echo
5.2 – Output for print in place of echo

Use the print_r(); by replacing echo ” ” ; fig 5.1 to get the same output fig 5.2.

Array

An array is generated using an array() function in PHP. The three types of arrays in PHP are –

6.1 - PHP Programming Basics: Indexed or Numeric Arrays
6.1 – PHP Programming Basics: Indexed or Numeric Arrays
6.2 - PHP Programming Basics: Output Indexed or Numeric Arrays
6.2 – PHP Programming Basics: Output Indexed or Numeric Arrays

Indexed or Numeric Array

An array with a numeric index where values are stored linearly fig 6.1 & 6.2.

7.1 - Associated Arrays
7.1 – Associated Arrays
7.2 - Output Associated Arrays
7.2 – Output Associated Arrays

Associated Arrays

An array with a string index where each value can be assigned a specific key fig 7.1 & 7.2.

8.1 - Multidimensional Arrays
8.1 – Multidimensional Arrays
8.2 - Output Multidimensional Arrays
8.2 – Output Multidimensional Arrays

Multidimensional Arrays

An array which contains single or multiple arrays within it and can be accessed with multiple indices fig 8.1 & 8.2.

Loops

Loops in PHP execute the same block of code a definite number of times. Four loop types are:

9.1 - PHP Programming Basics: "For Loop"
9.1 – PHP Programming Basics: “For Loop”
9,2 - PHP Programming Basics: Output "For Loop"
9,2 – PHP Programming Basics: Output “For Loop”

For Loop

run through a block of code a specified number of times fig 9.1 & 9.2.

for (init; condition; increment)
{
code to be executed;
}
9.1 - "For each Loop"
10.1 – “For each Loop”
9.2 - Output "for each loop"
10.2 – Output “for each loop”

For each Loop

run through a block of code for each element in an array fig 10.1 & 10.2.

foreach ($array as $value)
{
code to be executed;
}
10.1 - "While Loop"
11.1 – “While Loop”
10.2 - Output "While Loop"
11.2 – Output “While Loop”

while loop − runs through a block of code if and as long as a specified condition is true fig 11.1 & 11.2.

while (condition)
{
 code to be executed; 
} 
11.1 - "Do While Loop"
12.1 – “Do While Loop”
11.2 - Output "Do While Loop"
12.2 – Output “Do While Loop”

Do While Loop

runs through a block of code once and then repeats the loop as long as a special condition is true fig 12.1 & 12.2.

do
{
code to be executed;
}
while (condition);

Conditionals

Conditionals serve as an aid to decision making allowing PHP programmers to evaluate different conditions during a program on whether these conditions evaluate to true or false. These conditions and the actions associated with them are called a Conditional Statement. The different types of Conditional Statements are:

13.1 – if Statement
13.2 – Output if

if Statement

The if Statement: In an if Statement output will appear only when Condition must be true fig 13.1 & 13.2.

if (expression){
Statement…
}
14.1 – if-else Statement
14.2 – Output if else

if-else Statement

The if-else Statement: In an if-else Statement output will display in both the condition (if condition is true display this message otherwise display that message) fig 14.1 & 14.2.

if (expression{
Statement…
} else{
Statement…
}
if_elseif_else
15.1 – if_else-if_else Statement
Output if_elseif_else
15.2 – Output if_elseif_else

if-elseif-else Statement

The if-elseif-else Statement: In an if-else-if-else statement multiple if-else statements are bound so that the programmer can define actions for more than just two possible outcomes fig 15.1 & 15.2.

if(expression){
Statement…
}elseif(expression){
Statement…
}else{
Statement…
}
16.1 – Switch Statement
16.2 – Output Switch Statement

Switch Statement

The Switch Statement: is similar to a series of if statements on the same expression fig 16.1 & 16.2.

switch(expression){
case:
Statement…
default:
Statement…
}
17.1 - PHP Operators
17.1 – PHP Operators
17.2 - Output PHP Operators
17.2 – Output PHP Operators

PHP Operators

PHP operators are symbols used to perform operations fig 17.1 & 17.2. The different types of operators are:

Arithmetic Operators  (+, -, /); 
Array Operators ( ==, ===, != ); 
Assignment Operators (=, +=, *=); 
Comparison Operators (<>, < >); 
Conditional Operators (‘if’ and ‘else’); 
Increment/Decrement Operators (++$x, $x++, $x—); 
Logical Operators (and, or, &&); 
Spaceship Operators ($x < $y, $x != $y, $x >= $y); 
String Operators ( . , .=).
18.1 - functions
18.1 – functions
18.2 - Output functions
18.2 – Output functions

Functions & Methods

A function is a block of statements that is used repeatedly in a program. PHP has a lot of built-in functions fig 18.1 & 18.2. With PHP we can create our own. functions. When a function is created outside a class or an object it is called a function. When you create a function inside a class, it is a Method. A function gets executed by a call to action

function functionName()
{
code to be executed;
}

PHP Documents

Read about PHP documentation by clicking on PHP_docs.

PHP Programming Basics – Regular Expressions

The commonly used Regular Expression functions in PHP are:

19.1 - PHP Programming Basics: preg_match () regex
19.1 – PHP Programming Basics: preg_match () regex
19.2 - PHP Programming Basics: Output preg_match () regex
19.2 – PHP Programming Basics: Output preg_match () regex

preg_match – this function performs a pattern match on a string fig 19.1 & 19.2. It returns a value true if the match is found. If not then it returns a value false.

20.1 - preg_split () regex
20.1 – preg_split () regex
20.2 - Output preg_split () regex
20.2 – Output preg_split () regex

preg_split – this function performs a pattern match on a string fig 20.1 & 20.2. It splits the results into a numeric array.

21.1 - preg_replace () regex
21.1 – preg_replace () regex
21.2 - Output preg_replace () regex
21.2 – Output preg_replace () regex

preg_replace – this function performs a pattern match on a string fig 21.1 & 21.2. The specified text replaces the match.

Read more about Regular Expressions by clicking on Regex.

Form Handling

22.1 - PHP Programming Basics: Form Handling
22.1 – PHP Programming Basics: Form Handling

HTML Form shows input fields for name, email and submit button fig 22.1.

22.2 - PHP Programming Basics: Output Form Handling
22.2 – PHP Programming Basics: Output Form Handling

In the display, user enters his name, email and clicks the ‘submit’ button fig 22.2.

23.1 - greetingsdotphp
23.1 – greetingsdotphp

Then the form data is processed by sending it to PHP file ‘greetings.php’ fig 23.1.

23.2 Output greetingsdotphp
23.2 Output greetingsdotphp

With ‘echo the variables’ the submitted data is displayed fig 23.2 for ‘greetings.php’. Note that the submission is with the ‘POST’ method. Alternatively, the ‘GET’ method also produce the same result.

Digital Marketing Solutions

Thank you for subscribing.

Something went wrong.