Quick Reference Study Notes for PHP Programming (Advanced)

What is PHP?

PHP is a server side scripting language. that is used to develop Static websites or Dynamic websites or Web applications. PHP stands for Hypertext Preprocessor, that earlier stood for Personal Home Pages.

       PHP scripts can only be interpreted on a server that has PHP installed.The client computers accessing the PHP scripts require a web browser only. A PHP file contains PHP tags and ends with the extension ".php".

What is a Scripting Language?

A script is a set of programming instructions that are interpreted at runtime.

     A scripting language is a language that interprets scripts at runtime. Scripts are usually embedded in other software environments. The purpose of the scripts is usually to enhance the performance or perform routine tasks for an application. Server-side scripts are interpreted on the server while client-side scripts are interpreted by the client application.

         php is a server side script that is interpreted on the server while javascript is an example of a client-side script that is interpreted by the client browser. Both php and javascript can be embedded into html pages.

Programming Language Vs Scripting Language

 

Programming language

Scripting language

Has all the features needed to develop complete applications.

Mostly used for routine tasks

The code has to be compiled before it can be executed

The code is usually executed without compiling

Does not need to be embedded into other languages

Is usually embedded into other software environments.

 

What does PHP stand for?

PHP means - Personal Home Page, but it now stands for the recursive backronym PHP: Hypertext Preprocessor.

PHP code may be embedded into HTML code, or it can be used in combination with various web template systems, web content management system and web frameworks.

Php Syntax

 

What is PHP? Write your first PHP Program

A PHP file can also contain tags such as HTML and client side scripts such as JavaScript.

 

PHP BASICS

Including PHP in a File

 

<?php

//place PHP CODE HERE

?>

 

Writing Comments

 

// — Denotes comments that only span one line

# — Another way of producing single-line comments

/*...*/ — Everything between /* and */ is not executed, also works across several lines

 

Outputting Data

<?php

echo “<h1> PHP Cheat Sheet </h1>"; ?>

 

Writing PHP Functions function

 

function NameOfTheFunction() {

//place PHP code here

}

VARIABLES AND CONSTANTS

 

Defining Variables

 

<?php

$BlogPostTitle=”php”;

?>

 

Types of Data
 

 Data Type                                    

               Description

Integer

Integers are non-decimal numbers between -2,147,483,648 and , 147,483,647. They must have at least one digit and no decimal point. Can be in decimal, hexadecimal or octal.

Floats

This is the name for numbers with a decimal point or in exponential form.

Strings

This simply means text, we will talk about it in detail further below. Boolean values Meaning true/false statements.

Arrays

Arrays are variables that store several values. We will talk about them in detail further below.

Objects

Objects store both data and information on how to process it. Resources These are references to functions and resources outside of PHP.

NULL

A variable that is NULL doesn’t have any value.


 

Variable Scope

 

function myFunction() {

global $a, $b;

$b = $a - $b;

}

 

Predefined Variables

 

                     Variable       

                     Description

$GLOBALS

Used to access global variables from anywhere inside a PHP script.

$_SERVER

Contains information about the locations of headers, paths and scripts.

$_GET

Can collect data that was sent in the URL or submitted in an HTML form.

$_POST

Used to gather data from an HTML form and to pass variables.

$_REQUEST

Also collects data after submitting an HTML form

 

Variable-handling Functions


 

Functions

                 Description

boolval

Used to retrieve the boolean value of a variable

debug_zval_dump

Outputs a string representation of an internal zend value

empty

Checks whether a variable is empty or not

gettype

Retrieves the variable type

is_array

Checks whether a variable is an array

is_bool

Finds out if a variable is a boolean

unset

Unsets a variable

var_dump

Dumps information about a variable

isset

Determine if a variable has been set and is not NULL

print_r

Provides human-readable information about a variable

is_numeric

Find out if a variable is a number or a numeric string

floatval

Get the float value of a variable (doubleval is another possibility)

 

Constants

 

Constants

Description

define(name, value, true/false)

Aside from user-defined constants, there also a number of default PHP constants:

__FILE__

Is the full path and filename of the file

__DIR__

The directory of the file

__FUNCTION__

Name of the function

__NAMESPACE__

Name of the current namespace

 

PHP ARRAYS – GROUPED VALUES


 

Types of Arrays

Description

Indexed arrays

Arrays that have a numeric index

Associative arrays

Arrays where the keys are named

Multidimensional arrays

Arrays that contain one or more other arrays

 

Declaring an Array in PHP

 

<?php


 

$cms = array("WordPress", "Joomla", "Drupal");


 

echo "What is your favorite CMS? Is it " . $cms[0] . ", " . $cms[1] . " or " . $cms[2] . "?";

 

?>

 

Array Functions


 

Functions

Description

array_change_key_case

Changes all keys in an array to uppercase or lowercase

array_chunk

Splits an array into chunks

array_combine

Merges the keys from one array and the values from another into a new array

array_filter

Filters the elements of an array via a callback function

array_keys

Returns all keys or a subset of keys in an array

arsort

Sorts an associative array in descending order according to the value

asort

Sorts an associative array in ascending order according to the value

array_search

Searches the array for a given value and returns the first key if successful

array_push

Push one or several elements to the end of the array

count

Count all elements in an array, alternatively use sizeof

in_array

Checks if a value exists in an array

array_merge

Merge one or several arrays

 

array_key_exists

Checks if a specified key exists in an array, alternative: key_exists

array_diff

Compares arrays, returns the difference (values only)

 

PHP STRINGS


 

Strings

Description

Single quotes

This is the simplest way. Just wrap your text in ' markers and PHP will handle it as a string.

Double quotes

As an alternative you can use ". When you do, it’s possible to use the escape characters below to display special characters.

heredoc

Begin a string with <<< and an identifier, then put the string in a new line. Close it in another line by repeating the identifier. heredoc behaves like double-quoted strings.

nowdoc

Is what heredoc is for double-quoted strings but for single quotes.

 

It works the same way and eliminates the need for escape characters.


 

Escape Characters

 

Character

Description

\n

Linefeed

\r

Carriage return

\t

Horizontal tab

\v

Vertical tab

\f

Form feed

\e

Escape

\\

Backslash

 

String Functions


 

Functions

Description

addcslashes()

Returns a string with backslashes in front of specified characters

chr()

Returns a character from a specified ASCII value

count_chars()

Returns information about the characters in a string

echo()

Outputs one or several strings

explode()

Breaks down a string into an array

htmlentities()

Converts characters to HTML entities

htmlspecialchars()

Switches predefined characters to HTML entities

 

implode()

Retrieves a string from the elements of an array, same as join()

ltrim()

Removes spaces or other characters from the left side of a string

print()

Outputs one or several strings

str_replace()

Replaces specified characters in a string (case-sensitive)

str_split()

Splits strings into arrays

strcmp()

Binary safe string comparison (case sensitive)

strpos()

Returns the position of the first occurrence of a substring in a string (case sensitive)

substr()

Returns a specified part of a string

ucfirst()

Transforms the first character of a string to uppercase

 

PHP OPERATORS

 

Arithmetic Operator


 

  Operator

Description

Example

+

Adds two operands

A + B will give 30

-

Subtracts second operand from the first

A - B will give -10

*

Multiply both operands

A * B will give 200

/

Divide numerator by de-numerator

B / A will give 2

%

Modulus Operator and remainder of after an integer division

B % A will give 0

++

Increment operator, increases integer value by one

A++ will give 11

--

Decrement operator, decreases integer value by one

A-- will give 9

 

Comparison Operators

 

Operator

Description

Example

==

Checks if the value of two operands are equal or not, if yes then condition becomes true.

(A == B) is not true.

!=

Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

(A != B) is true.

>

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

(A > B) is not true.

<

Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

(A < B) is true.

>=

Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

(A >= B) is not true.

<=

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

(A <= B) is true.

 

Logical Operator


 

Operator

Description

Example

and

Called Logical AND operator. If both the operands are true then condition becomes true.

(A and B) is true.

or

Called Logical OR Operator. If any of the two operands are non zero then condition becomes true.

(A or B) is true.

&&

Called Logical AND operator. If both the operands are non zero then condition becomes true.

(A && B) is true.

||

Called Logical OR Operator. If any of the two operands are non zero then condition becomes true.

(A || B) is true.

!

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

!(A && B) is false.


 

Assignment Operator


 

Operator

Description

Example

=

Simple assignment operator, Assigns values from right side operands to left side operand

C = A + B will assign value of A + B into C

+=

Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand

C += A is equivalent to C = C + A

-=

Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand

C -= A is equivalent to C = C - A

*=

Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

C %= A is equivalent to C = C % A

 

Conditional Operator

 

Operator

Description

Example

? :

Conditional Expression

If Condition is true ? Then value X : Otherwise value Y

 

LOOPS IN PHP


 

Types Of Loop

Example

For Loop

for (starting counter value; ending counter value; increment by which to increase) {

// code to execute goes here

 

}

Foreach Loop

foreach ($InsertYourArrayName as $value) {

 

// code to execute goes here

 

}

Do..While Loop

do {

 
    • code to execute goes here;

 

} while (condition that must apply);

 

CONDITIONAL STATEMENTS


 

Statements

Example

If Statement

if (condition) {

 

// code to execute if condition is met

 

}

If..Else

if (condition) {

    • code to execute if condition is met } else {

    • code to execute if condition is not met

 

}

If..Elseif..Else

if (condition) {

 

// code to execute if condition is met

} elseif (condition) {

 
    • code to execute if this condition is met } else {

    • code to execute if none of the conditions are met

 

}

Switch Statement

switch (n) {

 

case x:

 

code to execute if n=x;

 

break;

 

case y:

 

code to execute if n=y;

 

break;

 

case z:

 

code to execute if n=z;

 

break;

 
  • add more cases as needed default:

code to execute if n is neither of the above;

 

}


*NOTE : "This study material is collected from multiple sources to make a quick refresh course available to students."

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. More info. I Agree