PHP Programming Basics

Spread the love
 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. Use MySQL database with PHP to build data-driven dynamic web pages. Know how algorithms work for data search and sorting, counting, etc. Construct a sturdy CMS with the most secure authentication. 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. Interact with databases and build complicated reusable applications in PHP5 using PHP OOP. Develop secure PHP web applications by addressing issues to prevent attacks.

Address security issues such as XSS, Injections, working with files, passwords, PHP configurations, etc. 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

fig1–PHP Programming Basics-XAMPP~for Mac download
fig1–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.

fig2–XAMPP-Control Panel~Manage Servers
fig2–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.

fig3a– PHP Programming Basics Code
fig3a– PHP Programming Basics Code
fig3b-Output PHP Code
fig3b-Output PHP 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.

fig4a–PHP Programming Basics~variablesdotphp
fig4a–PHP Programming Basics~variablesdotphp
fig4b–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 that you use to access the server.
fig5a-variablesdotphp~Using print in place of echo
fig5a-variablesdotphp~Using print in place of echo
fig5b-Output for print in place of echo
fig5b-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 –

fig6a–PHP Programming Basics~Indexed or Numeric Arrays
fig6a–PHP Programming Basics~Indexed or Numeric Arrays
fig6b–PHP Programming Basics-Output Indexed or Numeric Arrays
fig6b–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.

fig7a–Associated Arrays
fig7a–Associated Arrays
fig7b–Output Associated Arrays
fig7b–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.

fig8a–Multidimensional Arrays
fig8a–Multidimensional Arrays
fig8b–Output Multidimensional Arrays
fig8b–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:

fig9a–PHP Programming Basics~“For Loop”
fig9a–PHP Programming Basics~“For Loop”
fig9b–PHP Programming Basics~Output “For Loop”
fig9b–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;
}
fig10a–“For each Loop”
fig10a–“For each Loop”
fig10b-Output "for each Loop"
fig10b-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;
}
fig11a-'While Loop'
fig11a-‘While Loop’
fig11b-Output 'While Loop'
fig11b-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; 
} 
fig12a–“Do While Loop”
fig12a–“Do While Loop”
fig12b–Output “Do While Loop”
fig12b–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:

fig13a– if Statement
fig13a– if Statement
fig13b– Output if Statement
fig13b– Output if Statement

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…
}
fig14a~'if-else' Statement
fig14a~’if-else’ Statement
fig14b–Output 'if else' Statement
fig14b–Output ‘if else’ Statement

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…
}
fig15a–'if_else-if_else' Statement
fig15a–’if_else-if_else’ Statement
fig15b– Output 'if_elseif_else' Statement
fig15b– Output ‘if_elseif_else’ Statement

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…
}
fig16a– Switch Statement
fig16a– Switch Statement
fig16b– Output Switch Statement
fig16b– 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…
}
fig17a– PHP Operators
fig17a– PHP Operators
fig17b– Output PHP Operators
fig17b– 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 ( . , .=).
fig18a–functions
fig18a–functions
fig18b– Output functions
fig18b– 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:

fig19a– PHP Programming Basics~preg_match () regex
fig19a– PHP Programming Basics~preg_match () regex
fig19b– PHP Programming Basics~Output preg_match () regex
fig19b– 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.

fig20a–preg_split () regex
fig20a–preg_split () regex
fig20b-Output preg_split
fig20b-Output preg_split

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

fig21a– preg_replace () regex
fig21a– preg_replace () regex
fig21b–Output preg_replace () regex
fig21b–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

fig22a–PHP Programming Basics~Form Handling
fig22a–PHP Programming Basics~Form Handling

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

fig22b–PHP Programming Basics~Output Form Handling
fig22b–PHP Programming Basics~Output Form Handling

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

fig23a–greetingsdotphp
fig23a–greetingsdotphp

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

fig23b-Output greetingsdotphp
fig23b-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 produces the same result.