PHP – $_GET/$_POST Methods

Forms are the most commonly used to send and received enquiries on any website. So there are two methods which are mainly used to send information from browser client to the server.

The $_Get Method in PHP :

When we use $_GET method in any form, the browser will append all the form values in the page page URL as a query string. All the form values will be added after the (?) character and if there is any space in the fields values and browser will automatically will replace the space + sign and pass all the values on the form action page. You can see this using the following example:

Example:

The $_POST method in PHP

$_POST is another most important method to pass the form values to the server. In this method, all information is sent using the HTTP headers.

Example:

Difference between $_GET and $_POST methods

  • In $_GET method, all form values are sent as a query string instead in $_POST all values are sent using HTTP headers.
  • $_GET method has some limit (upto 1024 characters) in passing the form values instead $_POST method doesn’t have any restriction in variable passing.
  • $_GET method is not much secure because all values are sent as query string instead $_POST method is highly secured method.
  • $_GET method can be used to send binary data like images etc. instead $_POST can send all kind of data in a very secured way.

 

 

PHP – String

A string in PHP is a combination of characters like “Welcome to our website thePHPguide.com”

A string can be defined in four different ways:

  1. single quoted
  2. double quoted
  3. heredoc syntax
  4. nowdoc syntax (since PHP 5.3.0)

single quoted string in PHP

This is the simplest way to define any string. If you add any PHP variables then it will display the variable name as it is. It will never display the value of the variable in single quoted string in PHP.

Example

double quoted string in PHP

in double quoted string, we can also add the PHP variable and all PHP variables will be executed and displays the variable value.

Example

heredoc string in PHP

The heredoc string starts with <<<, after this an identifier is provided. We can also include PHP variables inside the heredoc string and it will display the variable value.

 

nowdoc string in PHP

the nowdoc string is a single quoted string but the nowdoc string is defined similar to the heredoc string. In nowdoc string, we can’t execute the PHP variables instead in heredoc string we can easily execute the PHP variables.

Example

 

 

PHP – Loops

Loops in PHP

In this chapter we will know what are loops and why we use loops in our program.

In any program loops play an important role to complete any specified task. Mainly loops are used to execute a block of program several times until it matches the specified condition.

In PHP, we use mainly four loop types given below:

for − loops repeat a block of code in a program to a predefined number of times.

while − loops first test the condition and then repeat a block of code in a program until a specified condition is true.

do…while − loops first repeat a block of code in a program and the conditional staement is tested at the end of the loop.

foreach − loops mainly used to retrieve the arrays elements and reapeat a block of program for each array element.

The for Loop in PHP

The “for loop” is mainly used when we know in advance that how many times we need to repeat the code of a program. In “for loop”, we need to define the starting and end point before starting the loop.

for loop SYNTAX

for loop parameters

loop initialization: we will initialize the loop counter value using a variable name and assign the numeric value.
loop counter: here we will define how many times loop will run.
increment counter: here we will define the loop increment counter.

You can understand this with the following example:

The while loop in PHP

The while loop in PHP executes a block of a program until the specified condition returns true.

while loop SYNTAX

Example

The do…while loop in PHP

The do…while loop will execute the block of code and then match the specified condition. The loop will be executed until the condition returns true results.

do…while loop SYNTAX

Example

The foreach loop in PHP

The foreach loop is used only on arrays and repeat the block of code for each array key pair.

The foreach loop SYNTAX

Example

 

 

PHP – switch Statement

“switch statement” is one of the best for program execution based on several conditions.

Syntax of “switch statement” in PHP

 

The “switch statement” will be used mainly to avoid the long tail of “if..elseif..else” statements. It will execute the program based on the parameter to passed “switch statement”. The passed parameter will be check with all the cases and if match found it will execute the program otherwise it will returns to the default statement. We use “break;” to stop the program to check other cases and we can speed up the program execution.

“switch statement” example

 

PHP – Conditional Statements

The main purpose of conditional statements in PHP is to perform different kind of actions based on different conditions.

Conditional Statements in PHP

while we write the program we always needs to do different tasks based on different conditions. And for that purpose we use the following conditional statements.

  • if statement – “if statement” is used when execute a program if the condition returns true.
  • if…else statement – In this statement any of the one program will be executed. The program first check the “if condition” and will be executed when returns TRUE otherwise “else” statement will be executed.
  • if…elseif….else statement – This statement is used in more than two conditions.
  • switch statement – The “switch statement” is used to avoid long conditions of “if..elseif..else” code.

The if Statement in PHP

The “if statement” will be executed once the condition return TRUE results.

Syntax of if statement in PHP

if statement example

The if…else Statement in PHP

In this statement the “if program” will be executed once the condition return TRUE results otherwise the else program will be executed.

Syntax of if…else statement in PHP

if…else statement example

The if…elseif….else Statement in PHP

If there are multiple “if..else” conditions then we use The “if…elseif….else” Statement

Syntax of if…elseif statement in PHP

if…elseif statement example

The switch Statement in PHP

We will explain the switch statement in details in our next chapter so that you can understand it well. So please check our switch statement chapter for this.

 

PHP – Operators

What are Operators in PHP?

As the name define, Operators are used for doing operations on PHP variables.

PHP devided the operators in the following categories:

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

Arithmetic operators in PHP

Arithmetic operators are used with numeric values to perform arithmetical operations like addition, subtraction, multiplication etc.

Operator Name Example
+ Addition $a + $b result will be sum of $a and $b
Subtraction $a + $b result will be difference of $a and $b
* Multiplication $a + $b result will be multiplication of $a and $b
/ Division $a + $b result will be quotient of $a and $b
% Modulus $a + $b result will be remainder of $a and $b
++ Increment operator $a++ result will be  $a+1
Decrement operator $a– result will be  $a-1

Comparison operators in PHP

The comparison operators in PHP are used to compare the values of two variables.

Operator Description Example
== Equal $a == $b Returns  true if $a is equal to $b
!= and <> Not equal $a != $b Returns  true if $a is not equal to $b
> Greater than $a == $b Returns  true if $a is greater than $b
< Less than $a == $b Returns  true if $a is less than $b
>= Greater than or equal to $a == $b Returns  true if $a is greater than or equal to $b
<= Less than or equal to $a == $b Returns  true if $a is less than or equal to $b

Assignment operators in PHP

The assignment operators in PHP are used to assign a value in a PHP variable.

Operator Description Example
= Simple assignment operator $a = $b + $c will assign value of $b + $c into $a
+= Add AND assignment operator $a += $b  will assign value of $a + $b into $a
-= Subtract AND assignment operator $a -= $b  will assign value of $a – $b into $a
*= Multiply AND assignment operator $a *= $b  will assign value of $a * $b into $a
/= Divide AND assignment operator $a /= $b  will assign value of $a / $b into $a
%= Modulus AND assignment operator $a %= $b  will assign value of $a % $b into $a

Logical operators in PHP

The logical operators in PHP are used to combine the conditional statements.

Operator Name Example
and AND operator “$a and $b” Returns true if both $a and $b are true
or OR Operator “$a and $b” Returns true if both $a or $b is true
&& AND operator “$a && $b” Returns true if both $a and $b are true
|| OR Operator “$a || $b” Returns true if both $a or $b is true
! NOT Operator !$a Returns true if $a is not true

Increment/Decrement operators in PHP

The Increment Operators are used in PHP to increase a variable’s value by 1.

The decrement operators are used in PHP to decrease a variable’s value by 1.

Operator Name Example
++$a Pre-increment First increments $a by one, and then returns $a.

(1+$a)

$a++ Post-increment First returns $a, then increments $a by one.

($a+1)

–$a Pre-decrement First decrements $a by one, and then returns $a.

(1-$a)

$a– Post-decrement First returns $a, and then decrements $a by one.

($a-1)

String operators in PHP

String operators are used in PHP to conbine two variables.

Operator Description Example
. Concatenation $str1 . $str2

Concatenation of $str1 and $str2

.= Concatenation assignment $str1 .= $str2

Appends $str1 to $str2

Array operators in PHP

Array operators are used in PHP to compare arrays.

Operator Name Example
+ Union $a + $b

Returns Union of $x and $y

== Equality Returns true if $a and $b have the same key pairs
=== Identity Returns true if $a and $b have the same key pairs in the same order and of the same types
!= Inequality Returns true if $a is not equal to $b
<> Inequality Returns true if $a is not equal to $b
!== Non-identity Returns true if $a is not identical to $b

PHP – Data Types

What is Data Types in PHP?

Data type are the variable which are used to store data. Multiple data types can be used to store different values.

PHP supports following data types:

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

All these data types has explained in a seperate chapter called PHP variables. So please go through the PHP variables chapter for more details.

PHP – Constants

A constant is an identifier for any value. As the also says, that the value can’t be changed during program execution. A constant is case-sensitive by default and by convention, a constant identifiers are always uppercase. A valid constant name should start with a letter or underscore, followed by any number of letters, numbers, or underscores. Any constants name can be used anywhere in the program if needs to print the same values on various parts of the program. As a PHP constant may contain a long string value so we don’t need to write same string values again and again. You just define the constant and call it whenever you need to print the value.

How to define a PHP Constants with constant() function

To define a constant in PHP you need to use the define() function and if you want to print the value of the constant, you just need to specify the constant name only and this will print the value autometically.

constant() example

What are the main differences between constants and variables in PHP

  • There is no need to use a dollar sign ($) before any constant, instead a variable name starts with a dollar sign.
  • Constants will be defined using the define() function instead for a variable we use (=) operator to assign the value.
  • Constants values can not be chaned anywhere in the program. Once you define the program it will display the same value instead for a variable, a new value can be assigned anywhere in the program.

Magic constants in PHP

PHP provides a large number of predefined constants with the default values which are called Magic constants. The Magic constants are case-insensitive.

Following are the PHP Magic constants

  1. __LINE__
    The current line number of the file.
  2. __FILE__
    The full path and filename of the file with symlinks resolved.
  3. __DIR__
    The directory of the file.
  4. __FUNCTION__
    The function name.
  5. __CLASS__
    The class name.
  6. __TRAIT__
    The trait name.
  7. __METHOD__
    The class method name.
  8. __NAMESPACE__
    The name of the current namespace.

PHP – echo Statement

“echo” and “print” statements in PHP

Both “echo” and “print” statements are used to display the output of any php variable and script. So in this chapter we will understand how we will use the “echo” and “print” statements in the php program and we will also see the differences in both “echo” and “print” statements.

Main differences between “echo” and “print” statements in PHP

As both “echo” and “print” are not actually a PHP functions instead these are the PHP language construct. So we are not required to use the parathenses while using these functions.

Here are the main differences in “echo” and “print”

1. echo print the output faster than print.

2. echo doesn’t return any value instead print returns a value of 1. So print can also be used in expressions.

3. echo can pass multiple parameters if needed instead print can pass only one parameter.

I hope the above three points clears the differences in both.

Example “echo” statement in PHP

Example “print” statement in PHP

 

 

PHP – Variables

Creating (Declaring) PHP Variables

Variables in PHP plays an important role in storing information and print the values whenever needed in the PHP program. Following are the few important things which you should know about the PHP variables:

  • In PHP, all the variables are represented by a dollar sign ($).
  • Values are assigned in any PHP variable using the Assignment Operators ( = )
  • All the variable names in PHP are case-sensitive.
  • In PHP, a valid variable name starts with a letter or underscore, followed by any number of numbers, letters, or underscores (A-z, 0-9, and _ ).
  • Variables are used to store a number or a string of characters.

Example with PHP variables

Data Types in PHP

There are mainly eight data types in PHP and different data types can store different things. The list of all the data types is given below:

Integers : Integers are whole numbers and there will be no decimal points, like 99 etc.

Integers Example

Doubles : Doubles are floating-point numbers with decimal points, like 99.9 or 99.99 etc.

Strings : A strings is a combination of characters, like ‘PHP is used mainly for web development purpose.’

We can use two types of strings:
1. Single Quoted String – Using single quoted string all the values will be printed as you write instead
2. Double Quoted String – In double quoted string, all the PHP variables values will be replaced with the variable assigned values. So in doubled quoted string we can use PHP variables also to print dynamic values.

Strings Example

 

Booleans : Booleans can store only two values either it could be true or false.

Booleans Example

Arrays : Arrays can store multiple values in one variable. Arrays could be single dimentional or multi-dimentional.

Arrays Example

 

Objects : Objects are defined by the programmer using classes, which can be used with other functions within the same class.

Objects Example

 

Resources : Resources are used to hold references to resources external to PHP (like database connections etc).

NULL : NULL is a special data type whic can hold only one value: NULL.