My Experience And Explanation On PHP.

IBRAHIM HASHIR
7 min readAug 18, 2023

Php means personal home page or Hypertext Preprocessor.

PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages,

PHP files can contain text, HTML, CSS, JavaScript, and PHP code.

PHP files must have the extension of “.php”

  • PHP files have extension “.php”

PHP is easy to Learn, PHP has a straight forward and readable syntax, making it an excellent choice for beginners.

  • Server-Side Scripting: PHP is executed on the server call xampp, allowing you to perform complex tasks that cannot be achieved with client-side like HTML and others

GETTING STARTED WITH PHP AND WHAT WE NEED:

Before you start coding in PHP, you need a development environment. Here’s how to set it up:

- Install a Local Server: Download and install a web server with PHP support, such as XAMPP, WAMP, or MAMP, depending on your operating system.
- Code Editor: Choose a code editor, like Visual Studio Code, Sublime Text, for a more efficient coding experience.
- Create a PHP File: Save your PHP code with a .php extension, e.g., `index.php`.

PHP Syntax.

A PHP script is executed on the server, and the plain HTML result is sent back to the browser for the user, A PHP script can be placed anywhere in the document, A PHP script starts with <?php and ends with ?>:

<?php
// write your php code here.
?>

we use “echo" to output text in "Hello World!" on a web page:

example:

Note: PHP statements end with a semicolon (;).

PHP Case Sensitivity

In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.

In the example below, all three echo statements below are equal and legal

Note: However; all variable names are case-sensitive!

How to Comments in PHP:

A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.

to comment a single-line of code we make use two forward slash //

Example and Syntax for single-line comments:

This commented can't display on the browser

Syntax for multiple-line comments: /* */

Example:

Variables in PHP:

Variables are “containers” for storing information, In PHP, a variable starts with the $ sign, followed by the name of the variable.

Example:

After the execution of the statements above, the variable $firstname will hold the value “Ibrahim”, the variable $lastname will hold the value “Bolaji”, and the variable $username will hold the value “Ibtech”..

Note: When you assign a text value to a variable, put single quote or double quote around the value.

Some rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0–9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)

Output Variables

The PHP echo statement is often used to output data to the screen.

The following example will show how to output text and a variable:

check the example below:

this is the output.

PHP Data Types:

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • String, Integer, Float, Boolean, Array, Object, NULL, Resource

PHP String

A string is a sequence of characters, like “Hello String!”.

A string can be any text inside quotes. You can use single or double quotes:

example:

PHP Integer

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

Rules for integers:

  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation
  • The PHP var_dump() function returns the data type and value:..

PHP Float

A float (floating point number) is a number with a decimal point or a number in exponential form.

The PHP var_dump() function returns the data type and value::

PHP Boolean

A Boolean represents two possible states: TRUE or FALSE..

PHP Array

An array stores multiple values in one single variable.

PHP Constants

Constants are like variables except that once they are defined they cannot be changed or undefined.

To create a constant, use the define() function.

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

<?php
$x = 10;
$y = 6;

echo $x — $y;
?>

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is “=”. It means that the left operand gets set to the value of the assignment expression on the right.

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value.

The PHP decrement operators are used to decrement a variable’s value.

example :

$x = 10;
echo ++$x;

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

example:

(and, or, not…)

PHP Array Operators

The PHP array operators are used to compare arrays.

PHP String Operators

PHP has two operators that are specially designed for strings.

ARRAY IN PHP:

An array stores multiple values in one single variable:

we have three types of array in php:

PHP Indexed Arrays

There are two ways to create indexed arrays:

example:

$car = array(“Volvo”, “BMW”, “Toyota”);
echo “I like “ . $car[0] . “, “ . $car[1] . “ and “ . $car[2] . “.”;

PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.

There are two ways to create an associative array:

$age = array(“Peter”=>”35", “Ben”=>”37", “Joe”=>”43");
echo “Peter is “ . $age[‘Peter’] . “ years old.”;

PHP — Multidimensional Arrays

A multidimensional array is an array containing one or more arrays.

PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

loops in PHP:
-for: Execute a block of code for a fixed number of times.
- while: Repeat a block of code as long as a condition is true.
- foreach: Iterate over elements in an array or collection.

PHP Form Handling:

GET vs. POST In Php Form handling:

GET : data is appended to the URL.

Not secure:

char limit -> (2000 word)

GET requests can be cached.

Better for a search page.

POST: data packaged inside the body of the HTTS request .

More Secure:

No data limit

Better for submitting credentials .

Example:

PHP form action attribute is used to specify where the data is sent to be processed.

Superglobals $_POST and $_GET are used to gather data from PHP forms. GET methEFod is used for non-sensitive data and allows bookmarking pages

The isset() function in PHP is used to determine whether a variable is set or not. A variable is considered as a set variable if it has a value …

How to connect and to send data to database:

Before we can access data in the MySQL database, we need to be able to connect to the server:

check the below example:

linking php to MySQL Database

we need to create server name, user, database name and password:

How to create a database:

click on new and then click write the database name and click on submit

Table name:

--

--