11. Operator

Arithmetic operators: (+ Addition),(- subtraction), (* multiplication), (/ division),( % modulus)

Assignment Operators:   =

Comparison Operators:

  •     ==  equal to            [ echo ( 7 == “7” ) ] Only Data  check, if 7 equal 7 otherwise false
  •     ! = not equal to      [ echo (7 != 6)  ]  if 7 doesn’t equal 6, otherwise false
  •     === identical to     [ echo 6=== “6” ]  Data and Data type check. if 6 doesn’t equal 6 or they aren’t same type, otherwise false
  •     < less than                            [ echo 3<5 ]
  •     > greater than                      [ echo 5>4 ]
  •     <= less than equal to
  •     >= greater than equal to

Bitwise Operator:
Binary: Binary is a 0 , 1 and binary base is a 2.
How to get binary 255

1 Byte ( 8 bits )
256 128 64 32 16 8 4 2 1
8 7 6 5 4 3 2 1 0
2 2 2 2 2 2 2 2 2
128 255 1
64 127 1
32 83 1
16 51 1
8 35 1
4 23 1
2 21 1
1 20 1
128 64 32 16 8 4 2 1 total=255
1 1 1 1 1 1 1 1

Read More »

Advertisements

10. Parameters, argument and Superglobals

Parameters & argument: The parameters are what are used inside the function and the arguments are the values passed when the function is called.
So here, x and y would be formal parameters:
int foo(int x, int y) { … }
Whereas here, in the function call, 5 and z are the actual arguments:
foo(5, z);

Superglobals: These arrays are known as superglobals because they’re accessible from anywhere in your script — including inside functions — and you don’t need to declare them as global using the global keyword.

Here’s a full list of the superglobals available in PHP, as of version 5.3:

$GLOBALS         Contains a list of all global variables in the script

$_GET                 Holds a list of all form fields sent by the browser using the get request

$_POST              Holds a list of all form fields sent by the browser using the post request

$_COOKIE          Holds a list of all cookies sent by the browser

$_REQUEST       Contains all the keys and values in the $_GET, $_POST and $_COOKIE arrays combined

$_FILES              Holds a list of any files uploaded by the browser

$_SESSION        Allows you to store and retrieve persistent session variables for the current browser

$_SERVER         Holds server environment info such as the filename of the running script, and the IP address of the browser.

9. Function and Return

What is function: PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value. Function will be executed by a call to the function. The real power of PHP comes from its functions. it has more than 1000 built-in functions. Besides the built-in PHP functions, we can create our own functions.

A user defined function declaration starts with the word “function”.

<?php
	function writeMsg() {
		echo "Hello world!";
	}
	writeMsg(); // call the function
?>

Return: anything’s Return from function then other codes not run or execute.

8. Constants and Predefined Constants

Constants: A constant is an identifier for a simple value. A constant value cannot change during the execution of the script. By default, a constant is case-sensitive. constant identifiers are always uppercase. A constant name starts with a letter or underscore. If you have defined a constant, it can never be changed. To define a constant you have to use define() function

<?php
	define("GREETING", "Welcome to Constant area");
	echo GREETING;
?>

Predefined Constants:
PHP features a large number of built-in, predefined constants holding various useful values. Some of these are always available. For example, the PHP constant M_PI holds the mathematical constant Pi. The following code displays this value:

<?php
	echo M_PI;
?>

Another useful predefined constant is PHP_VERSION, which holds the current version of the running PHP engine:

<?php
	echo PHP_VERSION;
?>

7. Variable Variable and Static Variable

Variable Variable:

<?php
	$bd="Bangladesh";
	$$bd="Dhaka";
	echo $Bangladesh;
?>

Static variable:Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. A static variable will not lose its value when the function exits and will still hold that value should the function be called again

<?php
	function myTest() {
		static $x = 0;
		echo $x;
		$x++;
	}
	myTest();
	myTest();
	myTest();
?>

6. Variable Scope

  1. File to File Variable Scope
  2. Function to function variable scope

File to file variable scope:
Have 4 Functions (include, include_once, require, require_once). Where the difference between is include & require: if you have used include function and include file path didn’t match. You will see a warning msg. but next code is executed or run. if you have used require function and require file path didn’t match. You will see an error msg. but next code is not executed or run.

Variable scope with function: If you want to access outer variable of the function. you have  to use global function.

<?php
	/*$var=10;
	function ajob(){
		global $var;
		return $var;
	}
	echo ajob();
	*/
	$a=5; // This is global variable
	function test(){
		$a =6; // This is local variable
		return $a+$GLOBALS[‘a’];
	}
	echo test();
?>

5. A String literal different ways

  1. Single quoted
  2. Double quoted
  3. Nowdoc syntax
  4. Heredoc syntax

Single quoted & double quoted:

<?php
	
	echo 'They don\'t know how it\'s possible';
	echo "<pre>";
	
	$karim="student";
	$age = 20;
	echo 'karim is a $karim, he is $age years old';
	echo "<pre>";	
	echo "karim is a $karim, he is $age years old";

	// single quoted ' ' that is show only variable
	// double quoted " " that is show only value

?>

1.Nowdocs are to single-quoted strings.
2.Heredocs are to double-quoted strings.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nowdoc & heredoc</title>
</head>

<body>
<?php 
$name = "Ataur rahman";
$email = "ataur@gmail.com";
$mobile = "667688754";
$address = "Dhaka, Bangladesh";

echo <<< 'test'

<table width="403" border="1">
  <tr>
    <td width="162">Name</td>
    <td width="225">$name</td>
  </tr>
  <tr>
    <td>Email</td>
    <td>$email</td>
  </tr>
  <tr>
    <td>Mobile</td>
    <td>$mobile</td>
  </tr>
  <tr>
    <td>Address</td>
    <td>$address</td>
  </tr>
</table>

test;
?>
</body>
</html>

4. PHP Loose type and Type Casting

PHP Loose type:

echo “<pre>”;
var_dump($v=”profile”);
var_dump($v=6);
var_dump($v=6.5);
var_dump(false == array());
var_dump(true == array());

Data type changing with casting:

$a=6.5;
echo (integer) $a;      //Run time type casting, only this line type cast
echo $a;
echo $a;

but if you permanently type cast. So you have to do use settype function

Data type changing with settype():

$a=9.6;
settype($a, “integer”);
echo $a;
echo $a;
echo $a;               //Result not change

3. PHP Data Type

PHP Data Type: 2 data type

1. Scaler Data type:

Type

Description

Example values

integer  A whole number (32 bit) 15, -36
float  A floating point number (32 bits with fraction) 9.58, -65.58
string  A sequence of characters "World", "abc123@#$"
unicode  A sequence of Unicode characters "Hello", "abc123@#$"
binary  A sequence of binary (non-Unicode) characters "Hello", "abc123@#$"
boolean  Either true or false true, false

2. Compound Data Type:

Array : Can hold multiple values indexed by numbers or strings

Object: Can hold multiple values (properties), and can also contain methods

2. How much PHP Tags / Syntax? Echo and print?

<SCRIPT LANGUAGE='PHP'> </SCRIPT>

<% %> [enabled asp_tags php.ini setting]

<?php ?> [This is standard Tags]

<? ?> [enabled short_open_tag php.ini setting]

Echo and print: In PHP there are two basic ways to get output: echo and print. echo and print are more or less the same. They are both used to output data to the screen. echo and print is not actually a function (it is a language construct), echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.

<?php
	echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>  
<?php
	print "I'm about to learn PHP!";
?>