strpos, strrpos, substr


$string = "search the position of the first, search the position of the last inside the string";
$search = "position";
$pos = strpos($string, $search);
echo $pos;
echo "<br>";
echo "<br>";

$pos2 = strrpos($string, $search);
echo $pos2;
echo "<br>";
echo "<br>";

$return = substr($string, $pos);
echo $return;

echo "<br>";
echo "<br>";
$return2 = substr($string, $pos2);
echo $return2;

echo "<br>";
echo "<br>";
//Find the position of the last occurrence of "php" inside the string:
echo strrpos("I love php, I love php too!","php");

echo "<br>";
echo "<br>";

//Find the position of the first occurrence of "php" inside the string:
echo strpos("I love php, I love php too!","php");

20. Session

What is a PHP Session?
The web server does not know who you are or what you do, because the HTTP address doesn’t maintain state. Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.

So; Session variables hold information about one single user, and are available to all pages in one application.

Start a PHP Session
A session is started with the session_start() function.

Session variables are set with the PHP global variable: $_SESSION.

Example:

<?php
	session_start();
	$_SESSION['name']="Username";

	echo isset($_SESSION['name'])?$_SESSION['name']." is available":"The user not Exist";
	
?>

Destroy a PHP Session
To remove all global session variables and destroy the session, use session_unset() and session_destroy():

Example

<?php
	session_start();
	$_SESSION['name1']="Unsername";
	session_unset();
	session_destroy();
	echo isset($_SESSION['name1'])?$_SESSION['name1']." is available":"The user not Exist";
	
?>

18. Difference between GET and POST Request

Note that the query string (name/value pairs) is sent in the URL of a GET request:

http://127.0.0.1/ibcs/ataur/fresh/insert.php?id=3&name=Kabir

Some other notes on GET requests:

  • GET requests can be cached
  • GET requests remain in the browser history
  • GET requests can be bookmarked
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions
  • GET requests should be used only to retrieve data

Note that the query string (name/value pairs) is sent in the HTTP message body of a POST request:

POST /test/demo_form.asp HTTP/1.1

Some other notes on POST requests:

  • POST requests are never cached
  • POST requests do not remain in the browser history
  • POST requests cannot be bookmarked
  • POST requests have no restrictions on data length

17. isset and empty and Null

isset and empty are not actually regular functions but language constructs. That means they’re part of the PHP language itself.
bool isset( mixed $var [, mixed … ])
Returns true if the variable exists and is not null. Does not trigger an error if the variable does not exist.

<?php
	$foo = 'bar';
	var_dump(isset($foo));        //true

	$baz = null;
	var_dump(isset($baz));        //false

	var_dump(isset($undefined));  //false
?>
if ($_GET['var'] == 'foo')

If $_GET[‘var’] actually exists, that is, if the URL contains ?var=foo, this will work just fine, regardless of the value of var. The presence or absence of $_GET[‘var’] is entirely in the hand of the user though. If the user does not include ?var=foo in the URL, this program will trigger an error.

To avoid this, we need to use isset:

if (isset($_GET['var']) && $_GET['var'] == 'foo')

isset can accept multiple arguments and only returns true if all of them are set:

if (isset($foo, $_GET['bar'], $array['baz'])) {
    // all needed values exist, do something with them
}

bool empty( mixed $var )
Returns true if the variable does not exist or its value equals false in a loose comparison. Does not trigger an error if the variable does not exist.

<?php
	$foo = 'bar';
	var_dump(empty($foo));        // false

	$baz = null;
	var_dump(empty($baz));        // true

	var_dump(empty($undefined));  // true
?>

The important and remarkable thing about this behavior is that when trying to pass non-existent variables to normal functions, an error is triggered. PHP first tries to get the value of the variable, then pass it into the function:

  var_dump($foobar);   -> Notice: Undefined variable: foobar NULL

Null:
When talking about isset one inevitably must also talk about null. Let’s come back for a second to the predicament of PHP trying to skip over trivial errors without crashing a program.

<?php
	$foo = $_GET['var'];
	$bar = 'bar';
	if ($foo == $bar) {
		// do something
	}
?>

How should PHP behave in the above program if $_GET[‘var’] did not exist? Sure, it will throw a tantrum and trigger a notice, and that’s good and fine. But how should it treat $foo afterwards? As some sort of special outcast that doesn’t have a value? Should it trigger an error every time $foo is used hence? The answer is simple: PHP assigns the value null in place of the non-existent variable. null is a type unto its own. null is not a boolean, not an integer, not a string, not an object. null is of type null which can only have one value: null. null is used to mean the absence of a value, but null is just a regular value in itself. null loosely compares to false (null == false, but null !== false).

16. Array

Array: An array is a special type of variable that can hold many values at once all accessible via a single variable name.Arrays are very useful whenever you need to work with large amounts of data — such as records from a database — or group related data together.

How arrays work:

  • An array can hold any number of values.
  • Each value in an array is called an element.
  • You access each element via its index, which is a numeric or string value. Every element in an array has its own unique index.
  • An element can store any type of value, such as an integer, a string, or a Boolean.

There are three different kind of arrays:

  1. Numeric array − An array with a numeric index
  2. Associative array − An array with strings as index
  3. Multidimensional array or Nested Array − An array containing one or more arrays

Indexed Array or Numeric Array:
These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

Numeric array example:

 $arr=[3,5,7,9.5,true,"a","c","eeee"];        //latest Method
//$arr=array(3,5,7,9.7,true,"a","c","e");    //old Method
echo "<pre>";        //html tag
print_r($arr);      //function
//var_dump($arr);  // funcion
//echo $arr[6];   // If you want to access single element

for($i=0;$i<count($arr); $i++){  // If you want to access all element
//echo $arr[$i],"<br>";
}

Associative Array:
The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string.
To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

associative array example:

$arr2=['a'=>'America','b'=>'Bangladesh','c'=>'China'];                   //latest Method
//$arr2=array('a'=>'America','b'=>'Bangladesh','c'=>'China','d'=>4);    //old Method
echo "<pre>";
//print_r($arr2);

/*foreach loop*/  
$arr3=array('a'=>'America','b'=>'Bangladesh','c'=>'China','d'=>4);
echo "<pre>";
foreach($arr3 as $a){           // If you want to only value, for this
	//echo $a,"<br>"; 
}

foreach($arr3 as $k=>$a){      // If you want to access index, for this 
	//echo $k,"<br>";
}

foreach($arr3 as $k=>$a){     // index and value 
	//echo $k,"=>", $a,"<br>";
}

Hybrid array Example:

$arr4=[4,5,6,'a'=>'America','b'=>'Bangladesh'];
//print_r($arr4);

Multidimensional Arrays or nested Arrays:
A multi-dimensional 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. Values in the multi-dimensional array are accessed using multiple index.
Example:

$arr6=[2,4,5=>6,9,8,3=>'a','b'];
//echo $arr6[8];
	
$multi=[4,5,3=>[1,2,9],['a','b','c'=>['x','y','z']]];
//echo $multi[4]['c'][2];

$multip=[[[[[[[10],9]]]]],11,12,13=>[15]];
echo $multip[0][0][0][0][0][1];
print_r($multip);

Array Function:

// extract function used with array
	$arr8=['u'=>'University','i'=>'index','m'=>'model'];
	extract($arr8); // if you want to convert associative array index as variable
	//echo $m;
	
// EXTR_PREFIX_ALL function used with array
	$arr9=[5,8,9,10];
	extract($arr9,EXTR_PREFIX_ALL,'b');
	echo $b_2;
	
// Normally numerical array's index does not converted in variable, because these index is number.but if you want to convert numeric index as variable, You can use function.
	

15. continue and break

Continue: In the following example loop prints the value but for which condition becomes true it just skip the code and next value is printed.

break: The PHP break keyword is used to terminate the execution of a loop prematurely.

while ($foo) {
continue;            // return back loop
break ;                // stop line
}

Example: 1

<?php
	for($j = 1; $j <= 10; $j++){
		if($j == 6){
			continue;
		}
		echo $j."<br>";
	}
?>


Example: 2

<?php
	for($j = 1; $j <= 10; $j++){
		if($j == 6){
			break;
		}
		echo $j."<br>";
	}
?>

14. Switch

If you want to select one of many blocks of code to be executed, use the Switch statement.
The switch statement is used to avoid long blocks of if..elseif..else code.

<?php
	$k=8;
	switch ($k){

		case $k>1 && $k<2:
		echo "one";
		break;
		 
		case 3:
		echo "Three";
		break;
		 
		case 5:
		echo "Five";
		break;
		 
		case 7:
		echo "seven";
		break;
		 
		case 20;
		echo "Twenty";
		break;
		 
		default:
		echo "nothing";
		break;
	}
?>

13. Loops

In computer programming, a loop is a sequence of instruction that is continually repeated until a certain condition is reached.

  1. for
  2. foreach
  3. while
  4. do….while
for: Example

<?php
    
    for($i=1; $i<=10; $i++){
    //echo $i,”<br>”;
    }
    //echo “Reverse LOOP<br>”;
    for($i=10; $i>=1; $i––){
    //echo $i,”<br>”;
    }
    for($i=1; $i<=10; $i++){
    //echo $i*$i,”<br>”;
    }
    for($k=1; $k<6; $k++){
		static $r=1; // 1,2,6,24,120
		//$r=$r*$k;
    }
    //echo $r;
    for($a=6; $a>0; $a--){
		static $k = 1; //6,30,120,360,720
		$k = $a*$k; 
	}
	echo $k;
	
	
?>

while Example:

<?php
	$i=1;
	while($i<=10){
	//echo $i;
	$i++;
	}
	$i=10;
	while($i>=1){
	//echo $i,”<br>”;
	$i–-;
	}
?>

do….while example:

<?php
	$i=5;
	do {
		echo $i 
		$i++;  
	}
	while($i>6);
?>
// if while loop & for loop don’t meet any condition, don’t give output
// do….while loop uses for at least 1 output.
foreach Example:

12. Logical Operator

Logical Operators:

&& and a && b true if a and b are true, otherwise false
|| or a || b true if a or b are true, otherwise false
xor xor a xor b true if a or b — but not both — are true, otherwise false
! not !a true if a is false; false if a is true
Example-1

<?php
	$x=5;
	if($x>5 || $x<5){
		echo “five”;
	}
	else {
		echo “nothing”;
	}
?>
Example-2

<?php
	$x=5;
	if($x>5 || $x<5){
		echo “five”;
	}
	elseif($x==5 && $x==6) {
		echo “This is true”;
	}
	else {
		echo “nothing”;
	}
?>
Example-3

<?php
	$x=5;
	if($x!=6){   // isn’t 6
		echo “Five”;
	}
	else {
		echo “nothing”;
	}
?>
Example-4

<?php
	$x=35;
	if($x>0 && $x<=12){
		echo "You’re Child";
	}
	elseif($x>12 && $x<=16){
		echo "You’re Young";
	}
	elseif($x>16 && $x<=40){
		echo "You’re Adult";
	}
	else{
		echo "You’re Old";
	}
?>