PHP

PHP: Hypertext Preprocessor is open source server-side scripting language that is widely used for web development. PHP scripts are executed on the server. PHP allows writing dynamically generated web pages efficiently and quickly.

It is widely used,Open source scripting language.

We denote the php file with “.php” extension.
PHP Files can contain text, HTML, CSS, JAvascript and php code.

WHY PHP USE?

  • To generate dynamic page content
  • Runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • Compatible with almost all servers used today (Apache, IIS, etc.)
  • With PHP We create, open, read, write, delete, and close files on the server
  • PHP can collect form data
  • It can send and receive cookies
  • It is used to add, delete, modify data in your database
  • To control user-access

Syntax of PHP:

<?php
// PHP code goes here
?>

Note: PHP script starts with <?php and ends with ?> and file extension for PHP files is “.php”.

Example:

<!DOCTYPE html>
<html>
<body>
<?php
echo “Hello PHP User”;
?>
</body>
</html>


Comments in PHP:

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

<?php
// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/

// You can also use comments to leave out parts of a code line
?>


Echo vs. print statement.

echo and print are more or less the same.

echo() and print() are language constructs in PHP, both are used to output strings. The speed of both statements is almost the same.

echo() can take multiple expressions

Example:

<?php
echo “<h2>PHP is Fun!</h2>”;
echo “Hello world!<br>”;
echo “I’m about to learn PHP!<br>”;
echo “This “, “string “, “was “, “made “, “with multiple parameters.”;
?>

Print Statement:

The print statement can be used with or without parentheses: print or print().

Print return true or false based on success or failure whereas echo doesn’t return true or false.

print cannot take multiple expressions.

Example:

<?php
print “<h2>PHP is Fun!</h2>”;
print “Hello world!<br>”;
print “I’m about to learn PHP!”;
?>


 

$message vs. $$message in PHP.

$message is a variable with a fixed name. $$message is a variable whose name is stored in $message.

If $message contains “var”, $$message is the same as $var.

—————————————————————————————————————————

Type of arrray:
1.single
2. two dimensional
3. multi- dimensionnal

1. single dimensional array:

$array=array(“men”,”women”,”child”);

$array[0]=”men”;
$array[1]=”women”;
$array[2]=”child”;

2. two dimennsional array:

$array1=array(
array(“men”,”women”,”child”);
);

echo $array1[0][0]=”men”;
echo $array1[0][1]=”women”;
echo $array1[0][2]=”child”;

3. mutli-dimensional array:

$array1=array(
array(“men”,”women”,”child”),
array(“cow”,”elephant”,”dog”),
array(“apple”,”banana”,”mango”)
);

$array1[0][0]=”men” $array1[0][1]=”women” $array1[0][2]=”Child”

$array1[1][0]=”cow” $array1[1][1]=”elephant” $array1[1][2]=”dog”

$array1[2][0]=”apple” $array1[2][1]=”banana” $array1[2][2]=”mango”