PHP for and foreach loops

The for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for (init; condition; increment)
  {
  code to be executed;
  }
Parameters:
  • init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
  • condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment: Mostly used to increment a counter (but can be any code to be executed at the end of the iteration)
Note: The init and increment parameters above can be empty or have multiple expressions (separated by commas).

Example

The example below defines a loop that starts with i=1. The loop will continue to run as long as the variable i is less than, or equal to 5. The variable i will increase by 1 each time the loop runs:


<?php
for ($i=1; $i<=5; $i++)
  {
  echo "The number is " . $i . "<br>";
  }
?>



Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5


The foreach Loop

The foreach loop is used to loop through arrays.

Syntax

foreach ($array as $value)
  {
  code to be executed;
  } 
 
For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.

Example

The following example demonstrates a loop that will print the values of the given array:


<?php
$x=array("one","two","three");
foreach ($x as $value)
  {
  echo $value . "<br>";
  }
?>



Output:
one
two
three

PHP while loop

The while Loop

The while loop executes a block of code while a condition is true.

Syntax

while (condition)
  {
  code to be executed;
  }

Example

The example below first sets a variable i to 1 ($i=1;).
Then, the while loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:


<?php
$i=1;
while($i<=5)
  {
  echo "The number is " . $i . "<br>";
  $i++;
  }
?>

 
 
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5


The do...while Statement

The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.

Syntax

do
  {
  code to be executed;
 
}
while (condition);

Example

The example below first sets a variable i to 1 ($i=1;).
Then, it starts the do...while loop. The loop will increment the variable i with 1, and then write some output. Then the condition is checked (is i less than, or equal to 5), and the loop will continue to run as long as i is less than, or equal to 5:
 
 
<?php
$i=1;
do
  {
  $i++;
  echo "The number is " . $i . "<br>";
  }
while ($i<=5);
?>

Output:
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6

PHP Arrays


What is an Array?

An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

$cars1="Saab";
$cars2="Volvo";
$cars3="BMW"; 
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is to use an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.
In PHP, there are three types of arrays:
  • Numeric array - Arrays with numeric index
  • Associative array - Arrays with named keys
  • Multidimensional array - An array containing one or more arrays

PHP Numeric Arrays

There are two ways to create a numeric array:
The index can be assigned automatically (index always starts at 0):
$cars=array("Saab","Volvo","BMW","Toyota");
OR   the index can be assigned manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";

Example


 <?php
$cars=array("Saab","Volvo","BMW","Toyota"); 

echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
OR
<?php
$cars[0]="Volvo";
$cars[1]="BMW";
$cars[2]="Toyota";
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?> 

OUTPUT: I like Volvo, BMW and Toyota.

 

 

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");

OR

$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
The named keys can then be used in a script:

Example

 <?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
OR
<?php
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
echo "Peter is " . $age['Peter'] . " years old.";
?>

OUTPUT: Peter is 35 years old.


 

Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Example

In this example we create a multidimensional array, with automatically assigned ID keys:
$families = array
  (
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
  );
The array above would look like this if written to the output:
Array
(
[Griffin] => Array
  (
  [0] => Peter
  [1] => Lois
  [2] => Megan
  )
[Quagmire] => Array
  (
  [0] => Glenn
  )
[Brown] => Array
  (
  [0] => Cleveland
  [1] => Loretta
  [2] => Junior
  )
)

echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";


OUTPUT: Is Megan a part of the Griffin family?

PHP Switch Statement

The PHP Switch Statement

Use the switch statement to select one of many blocks of code to be executed.

Syntax

switch (n)
{
case label1:
  code to be executed if n=label1;
  break;
case label2:
  code to be executed if n=label2;
  break;
default:
  code to be executed if n is different from both label1 and label2;
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.

Example

<?php
$favcolor="red";
switch ($favcolor)
{
case "red":
  echo "Your favorite color is red!";
  break;
case "blue":
  echo "Your favorite color is blue!";
  break;
case "green":
  echo "Your favorite color is green!";
  break;
default:
  echo "Your favorite color is neither red, blue, or green!";
}
?> 
 

PHP If...Else Statements

PHP - The if Statement

The if statement is used to execute some code only if a specified condition is true.

Syntax

if (condition)
  {
  code to be executed if condition is true
;
 
}
The example below will output "Have a good day!" if the current time is less than 20:

Example

<?php
$m=date("H");
if ($m<"20")
  {
  echo "Have a good day!";
  }
?>


//In the above Script The word "H" denotes Hours and the variable "m" assigned to H.

PHP - The if...else Statement

Use the if....else statement to execute some code if a condition is true and another code if the condition is false.

Syntax

if (condition)
  {
  code to be executed if condition is true;
 
}
else
  {
  code to be executed if condition is false;
 
}
The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:

Example

<?php
$m=date("H");
if ($m<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }
?>



//In the above Script The word "H" denotes Hours and the variable "m" assigned to H.


PHP - The if...else if....else Statement

Use the if....else if...else statement to select one of several blocks of code to be executed.

Syntax

if (condition)
  {
  code to be executed if condition is true;
 
}
else if (condition)
  {
  code to be executed if condition is true;
 
}
else
  {
  code to be executed if condition is false;
  }
The example below will output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":

Example

<?php
$m=date("H");
if ($m<"10")
  {
  echo "Have a good morning!";
  }
else if ($m<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }
?> 
 
 
//In the above Script The word "H" denotes Hours and the variable "m" assigned to H.
 
 

PHP - The if Statement

The if statement is used to execute some code only if a specified condition is true.

Syntax

if (condition)
  {
  code to be executed if condition is true
;
 
}
The example below will output "Have a good day!" if the current time is less than 20:

Example

<?php
$t=date("H");
if ($t<"20")
  {
  echo "Have a good day!";
  }
?>

Show example »


PHP - The if...else Statement

Use the if....else statement to execute some code if a condition is true and another code if the condition is false.

Syntax

if (condition)
  {
  code to be executed if condition is true;
 
}
else
  {
  code to be executed if condition is false;
 
}
The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:

Example

<?php
$t=date("H");
if ($t<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }
?>

Show example »


PHP - The if...else if....else Statement

Use the if....else if...else statement to select one of several blocks of code to be executed.

Syntax

if (condition)
  {
  code to be executed if condition is true;
 
}
else if (condition)
  {
  code to be executed if condition is true;
 
}
else
  {
  code to be executed if condition is false;
  }
The example below will output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":

Example

<?php
$t=date("H");
if ($t<"10")
  {
  echo "Have a good morning!";
  }
else if ($t<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }
?>

PHP mathematical operators


PHP is quite simple language as  using operators. its almost like to be same as C , C++ operators.
In PHP by using operators You can form a Calculator.....

Following are the operators available in PHP:

The assignment operator = is used to assign values to variables in PHP.

PHP Arithmetic Operators

Operator Name Description Example Result
x + y Addition Sum of x and y 2 + 2 4
x - y Subtraction Difference of x and y 5 - 2 3
x * y Multiplication Product of x and y 5 * 2 10
x / y Division Quotient of x and y 15 / 5 3
x % y Modulus Remainder of x divided by y
10 % 8
10 % 2

2
0
- x Negation Opposite of x - 2  
a . b Concatenation Concatenate two strings "Hi" . "Ha" HiHa

 

PHP Assignment Operators

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the expression on the right. That is, the value of "$x = 5" is 5.
Assignment Same as... Description
x = y x = y The left operand gets set to the value of the expression on the right
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus
a .= b a = a . b Concatenate two strings

 

PHP Incrementing/Decrementing Operators

Operator Name Description
++ x Pre-increment Increments x by one, then returns x
x ++ Post-increment Returns x, then increments x by one
-- x Pre-decrement Decrements x by one, then returns x
x -- Post-decrement Returns x, then decrements x by one

 

PHP Comparison Operators

Comparison operators allows you to compare two values:
Operator Name Description Example
x == y Equal True if x is equal to y 5==8 returns false
x === y Identical True if x is equal to y, and they are of same type 5==="5" returns false
x != y Not equal True if x is not equal to y 5!=8 returns true
x <> y Not equal True if x is not equal to y 5<>8 returns true
x !== y Not identical True if x is not equal to y, or they are not of same type 5!=="5" returns true
x > y Greater than True if x is greater than y 5>8 returns false
x < y Less than True if x is less than y 5<8 returns true
x >= y Greater than or equal to True if x is greater than or equal to y 5>=8 returns false
x <= y Less than or equal to True if x is less than or equal to y 5<=8 returns true

 

PHP Logical Operators

Operator Name Description Example
x and y And True if both x and y are true x=6
y=3
(x < 10 and y > 1) returns true
x or y Or True if either or both x and y are true x=6
y=3
(x==6 or y==5) returns true
x xor y Xor True if either x or y is true, but not both x=6
y=3
(x==6 xor y==3) returns false
x && y And True if both x and y are true x=6
y=3
(x < 10 && y > 1) returns true
x || y Or True if either or both x and y are true x=6
y=3
(x==5 || y==5) returns false
! x Not True if x is not true x=6
y=3
!(x==y) returns true

 

PHP Array Operators

Operator Name Description
x + y Union Union of x and y
x == y Equality True if x and y have the same key/value pairs
x === y Identity True if x and y have the same key/value pairs in the same order and are of the same type
x != y Inequality True if x is not equal to y
x <> y Inequality True if x is not equal to y
x !== y Non-identity True if x is not identical to y     

How Update avg in offline Mode

Procedure to Update AVG Offline

First, get the latest virus definition updates from here using another computer that has internet access and put a copy of the virus definition file in a USB stick or a CD.
Then, in the computer that does not have an internet connection but has the AVG antivirus software that has to be updated offline, right click on the system tray icon for AVG and click ‘Open AVG User Interface’. The AVG Antivirus Window should open up.
avg-offline-update-1
In the menu bar, choose Tools > Update from directory to update AVG offline.
avg-offline-update-2
A new window pops up. Choose the folder where the virus definitions are located. Click OK. AVG will use this file to update its virus database offline.
avg-offline-update-3
When the offline update is complete, AVG will show a confirmation, as shown below. You’re done.
avg-offline-update-4
So that’s how you can update AVG offline. If you’ve got any questions, feel free to post them in the comments.

PHP String Variables

The PHP Concatenation Operator.

There is only one string operator in PHP.
The concatenation operator (.)  is used to join two string values together.
The example below shows how to concatenate two string variables together:

Example

<?php
$txt1="Hello friends!";
$txt2="what about you!";
echo $txt1 . " " . $txt2;
?> 
 
 OUTPUT: Hello friends! what about you! 

Sometimes this operator perform a most importent role in our PHP script when we have a number of variables then with the help of this variable we can describe them in small portion

The PHP strlen() function

Sometimes it is useful to know the length of a string value.
The strlen() function returns the length of a string, in characters.
The example below returns the length of the string "Hello world!":

Example

<?php
echo strlen("Hello friends!");
?> 
 OUTPUT: 14

This script becomes very useful when you needs to count the total number of words in your PHP page.  for eg: name , Password length excitation.
 

PHP Variables

Creating (Declaring) PHP Variables

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
$txt="Hello world!";
$x=5;
After the execution of the statements above, the variable txt will hold the value Hello world!, and the variable x will hold the value 5.

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must begin with a letter or the underscore character
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name should not contain spaces
  • Variable names are case sensitive ($y and $Y are two different variables)

PHP varialbles are the main Parts of a PHP script.
In this Script we will assign our all components for eg. name,age,password,email etc...

Now using PHP Variables we are going to make a Simple Addition Function Which is Shown Below:

<?php
 

$x=5;
$y=6;
$z=$x+$y;
echo $z;


?> 


OUTPUT: 15



Note:-  In this PHP script x and y becomes variables beacuse we start them with $ letter. 

PHP Installation

What Do I Need?

To start using PHP, you can:
  • *Download This software and visualise The PHP On your Computer even in offline mode
  • Find a web host with PHP and MySQL support
  • Install a web server on your own PC, and then install PHP and MySQ

Use a Web Host With PHP Support

If your server has activated support for PHP you do not need to do anything.
Just create some .php files, place them in your web directory, and the server will automatically parse them for you.
You do not need to compile anything or install any extra tools.
Because PHP is free, most web hosts offer PHP support.

Set Up PHP on Your Own PC

However, if your server does not support PHP, you must:
  • Insatll the software i provided above    OR
  • install a web server
  • install PHP
  • install a database, such as MySQL
The official PHP website (PHP.net) has installation instructions for PHP: http://php.net/manual/en/install.php

PHP Introduction

Before you continue you should have a basic understanding of the following:

  • HTML
  • JavaScript
If you want to study these subjects first, You can also Learn these Languages from My home Page.

What is PHP?

  • PHP is simple for beginners.
  • PHP also offers many advanced features for professional programmers.
  • PHP stands for PHP: Hypertext Preprocessor
  • PHP is a widely-used, open source scripting language
  • PHP scripts are executed on the server
  • PHP is free to download and use

What is a PHP extension?

  • PHP files can contain text, HTML, JavaScript code, and PHP code
  • PHP code are executed on the server, and the result is returned to the browser as plain HTML
  • PHP files have a default file extension of ".php"

What Can PHP Do?

  • PHP can generate dynamic page content
  • PHP can create, open, read, write, and close files on the server
  • PHP can collect form data
  • PHP can send and receive cookies
  • PHP can add, delete, modify data in your database
  • PHP can restrict users to access some pages on your website
  • PHP can encrypt data
  • With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.

Why PHP?

  • PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP has support for a wide range of databases
  • PHP is free. Download it from the official PHP resource: www.php.net
  • PHP is easy to learn and runs efficiently on the server side



PHP Basics

So now I will goes to teach you PHP starting from basics..
To show a particular text On your page Just See below...



<?php


echo "My first PHP script! By modernhackerss.blogspot.com";


?>


OUTPUT : -



  My first PHP script! By modernhackerss.blogspot.com


Suggestions:- Php extensions always Start with "<?php" command and Ends with "?>" and every line is ended with " ; "
Try this many times so tha basics of PHP will Stay in your Mind.






How change windows Xp Bootscreen

Php home




Modernhackerss.blogspot.com

PHP is a server scripting language, and is a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

PHP stands for Hypertext Preprocessor and is a server-side programming language.  
There are many reasons to use PHP for server side programming, firstly it is a free language with no licensing fees so the cost of using it is minimal.
A good benefit of using PHP is that it can interact with many different database languages including MySQL. We work with MySQL at Bluelinemedia since this is also a free language so it makes sense to use PHP. Both PHP and MySQL are compatible with an Apache server which is also free to license. PHP can also run on Windows, Linux and Unix servers.

Due to all these languages being free it is cheap and easy to setup and create a website using PHP.
PHP also has very good online documentation with a good framework of functions in place. This makes the language relatively easy to learn and very well supported online. There are countless forums and tutorials on various PHP methods and problems so it is usually very easy to find help if you need it.
Due to PHP being so accessible and cheap to setup there are a lot of people who know how to use the language which makes finding new employees proficient in this language less challenging.
Those are the main reasons we use PHP