Category Archives: php

PHP Looping & Iteration

Looping & Iteration statements are used to execute the block of code repeatedly for a specified number of times or until it meets a specified condition. PHP supports four loop types.

The for loop in PHP

The for loop is used when we want to execute block of code known times.

Property Description
initialization Sets a counter variable
condition It test the each loop iteration for a condition. If it returns TRUE, the loop continues. If it returns FALSE, the loop ends.
increment Increment counter variable

The while loop in PHP

The while loop will execute a block of statement as long as a test expression is true.

The do…while loop in PHP

The do…while statement will execute a block of code and then test the condition for next iteration and executes next only if condition is true, block of code executes at least once.

The foreach Loop in PHP

The foreach statement is used to iterate over all elements of an array. It executes same piece of code for every element of an array

 

PHP Decision Making

Decision Making Statement allows computer to decide block of code to be execute based on the condition.if, if … else,if … else if and switch statements are used as decision making statements in PHP.

The if Statement in PHP

Block of statement executed only when a specified condition is true.

The If…Else in PHP

When we want to execute some block of code if a condition is true and another block of code if a condition is false, In such a case we use if….else statement.

The if … else if Statement in PHP

The if….elseif…else statement is used to select one of among several blocks of code to be executed.

The Switch case statement in PHP

The switch statement is simplified form of the nested if … else if statement , it avoids long blocks of if..elseif..else code.

 

PHP Constants

A constants is used for defining immutable values.Constant’s value cannot be modified during the execution of script.Constant name starts with a letter or underscore (dollar($) sign is not required before the name) and by convention constant names are always uppercase.

Constants are defined by using the define() function and its value can be accessed simply specifying its name.You can also use the function constant() to read a constant’s value.

Example:

Constants v/s Variables:

  • No need to write a dollar sign ($) before a constant name.
  • May not be redefined or undefined after defining it.
  • Constants cannot be defined by assignment statement, they can be only defined using the define() function.
  • Can be accessed globally

 

PHP Data Types

PHP supports total of eight data types which we use to define our variables:

  • Integers: Whole numbers, without a decimal point.
  • Doubles: Floating-point numbers, like 5.234 or 3.25.
  • Booleans: True or False.
  • NULL: Special type that only has one value: NULL.
  • Strings:Sequences of characters.
  • Arrays: An indexed collections values.
  • Objects: Instances of a user defined classes, which can encapsulate properties and data that are specific to the class.
  • Resources: Hold references to resources external to PHP (such as database connections).

PHP Syntax

PHP’s syntax are similar to most of the other programming languages (C, Java, Perl) with the addition that all PHP code is contained within a tag. PHP code can be easily embed into your HTML code.

Four different ways to escaping to PHP:

Note:- You can set the escaping option in your php.ini file.

Commenting in PHP Code:

PHP Statement termination:

Every statement in PHP must be followed by a semicolon (;). Block of valid PHP statements that is enclosed by the PHP tags is a valid PHP program.

PHP Case sensitivity:

In PHP the user defined functions, classes, core language keywords (for example if, else, while, echo etc.) are case-insensitive. PHP variables are case-sensitive

PHP Introduction

PHP stands for “Hypertext Pre-processor”.

PHP is most widely used server-side scripting language on the web, developed by Rasmus Lerdorf in 1994, designed for producing dynamic and interactive Web content. PHP originally stands for “Personal Home Page,” but as its features expanded, it became “PHP: Hypertext Processor”, As it processes web pages before they are sent to the browser. PHP page is processed first on the web-server and output creates an HTML which is sent to the web-browser.Although PHP is mostly used for Web-based applications, it can also be used for purpose of command-line scripting – a task usually handled by Perl.

PHP is…

  • Interpreted rather than compiled like Java or C.
  • An HTML-embedded scripting language.
  • A server-side technology; everything happens on the server as opposed to the Web browser’s computer, the client.
  • Cross-platform, meaning it can be used on Linux, Windows, Macintosh, etc., making PHP very portable.
  • PHP is better, faster, and easier to learn than the alternatives
  • An open source scripting language for web programming.

PHP requirements

The following elements represent the minimum requirements to begin working with PHP.
1. PHP enabled Web server.
2. If you are working on your own machine, you will need an SFTP client to transfer your PHP scripts to the Webserver.
3. Web browser (e.g., Mozilla Firefox, Apple Safari, Microsoft Windows Internet Explorer).

Example:

Our first PHP script “hello.php”

PHP is very popular if you are looking for a career in the web design/web development industry.

Prerequisites to learn PHP:

Starting with this tutorial it is important that you must have at least basic understanding of HTML, Computer programming, Internet, Database, and MySQL etc.