PHP

What is PHP ?

  • PHP is a server side scripting language.
  • PHP full name is PHP Hypertext Preprocessor
  • Php files have extention is .php
  • Php can generate dynamic page content
  • Php can create, open, read, write, delete, 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 runs on various platforms like Windows, Linux, Unix, Mac OS X, etc.
  • Php is compatible with almost all servers used today like Apache, IIS, etc.
  • Php 7 is much faster than the previous popular stable release (PHP 5.6)


PHP Syntax

A Php script can be placed anywhere in the document.

A Php script starts with <?php and ends with ?>:

<?php
// PHP code goes here
?>


Example

<html>
<body>

<?php
echo "Hello World!";
?>


</body>
</html>



Php statements end with a semicolon (;).

Comments :-

<html>
<body>

<?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
*/

?>


</body>
</html>



PHP Case Sensitivity


In Php, NO keywords (e.g. ifelsewhileecho, etc.), classes, functions, and user-defined functions are case-sensitive.

<html>
<body>

<?php
ECHO "Manu Dhaker<br>";
echo "Manu Dhaker<br>";
EcHo "Manu Dhaker<br>";
?>


</body>
</html>



But variables names are case sensetive.

<html>
<body>

<?php
$name = "manu dhaker";
echo "My name is " . $name . "<br>";
echo "My name is " . $NAME . "<br>";
?>


</body>
</html>


PHP Variables


<?php
$name = "Manu Dhaker";

$char = 'M';
$a = 5;
$b = 10.5;
?>



PHP Variables

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($name and $NAME are two different variables)
1)==>>
<html>
<body>

<?php
$name = "Manu Dhaker";
echo "I love $name!";
?>

</body>

</html>

2)==>><html>
<body>

<?php
$a = 5;
$b = 4;
echo $a + $b;
?>
</body>

</html>



PHP is a Loosely Typed Language




PHP Variables Scope


  • local
  • global
  • static


Global and Local Scope

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function...............................

<?php
$a = 5// global scope
function myTest() {
    // using a inside this function will generate an error    echo "<p>Variable a inside function is: $x</p>";

myTest();

echo "<p>Variable a outside function is: $a</p>";
?>



PHP global 


<?php
$a = 5;
$b = 10;

function myFunc() {
    global $a, $b;
    $b = $a + $b;
}

myFunc();
echo $b; // outputs 15?>



PHP static


<?php
function myFunc() {
    static $a = 0;
    echo $a;
    $a++;
}

myFunc();
myFunc();
myFunc();
?>


PHP echo and print Statements

  1. echo and print are more or less the same. 
  2. They are both used to output data to the screen.
  3. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. 
  4. echo can take multiple parameters (although such usage is rare) while print can take one argument. 
  5. echo is marginally faster than print.




<?php
echo "<h2>PHP!</h2>";
echo "This ""string ""was ""made ""with multiple parameters.";
?>



<?php
print "<h2>PHP</h2>";
print "I'm learning PHP!";
?>




PHP Data Types


  • String
  • Integer
  • Float (also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource


PHP String


A string is a sequence of characters, like "Manu Dhaker".......................

<?php 
$a = "Manu";
$b = 'Manu';

echo $a;
echo "<br>"
echo $b;
?>




PHP Integer


An integer data type is a non-decimal.
The PHP var_dump() function returns the data type and value................

<?php 
$a = 5;
var_dump($a);
?>

PHP Float


A float data type is a number with a decimal point

<?php 
$a = 4.789;
var_dump($a);
?>



PHP Boolean



<?php 
$a = true;

$b = false;
var_dump($a);

var_dump($b);
?>


PHP Array


An array stores multiple values in one single variable.

<?php 
$names = array("manu","kanu","trshu","drshu");
var_dump($names);
?>



PHP String Functions

  1. strlen()
  2. str_word_count() 
  3. strrev()
  4. strpos()
  5. str_replace()
<?php
echo strlen("Hello manu!"); // outputs 11?>


<?php
echo str_word_count("Hello manu!"); // outputs 2?>

<?php
echo strrev("Hello manu!"); // outputs !unam olleH?>

<?php
echo strpos("Hello manu!""world"); // outputs 6?>




<?php
echo str_replace("world""Manu""Hello world!"); // outputs Hello Dolly!?>



PHP Constants


Constants are like variables except that once they are defined they cannot be changed or undefined.

define(namevalue)

<?php
define("NAME""Manu Dhaker!");
echo NAME;
?>


<?php
define("NAME""Manu Dhaker!", true);
echo name;
?>



PHP Constant Arrays


In PHP7, you can create an Array constant using the define() function.

<?php
define("name", [
    "jenny",
    "dolly",
    "payal"
]);
echo name[0];
?>


Constants are automatically global and can be used across the entire script.



PHP Operators


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













Comments

Popular posts from this blog

C++ Language

C Language

Java Language