banner

How to search value from array using linear search and Object Oriented PHP?

SearchArray.php
<?php
	class SearchArray
	{
		function search($arr,$n,$no)
		{
			$pos=0;
			for($i=0;$i<$n;$i++)
			{
				if($arr[$i]==$no)
				{
					$pos=$i+1;
					$flag=true;
					break;
				}
			}
			return $pos;
		}
	}
?>
index.php
<?php
	require("SearchArray.php");
	$obj=new SearchArray();
	$array=array(1,2,3,4,5);
	$pos=$obj->search($array,5,1);
	if($pos>0)
		echo "Found at ".$pos." position";
	else
		echo "Not foud";
?>
banner

How to calculate addition, subtraction, multiplication and division using switch case statements in Object Oriented PHP?

<?php
	//Switch Case using class and object.
	class SwitchOop
	{
		function switchInitialize($ch,$a,$b)
		{
			switch($ch)
			{
				case 1:
					echo "The sum of ".$a." and ".$b." is ".($a+$b);
					break;
				case 2:
					echo "The subtraction of ".$a." and ".$b." is ".($a-$b);
					break;
				case 3:
					echo "The multiplication of ".$a." and ".$b." is ".($a*$b);
					break;
				case 4:
					echo "The division of ".$a." and ".$b." is ".($a/$b);
					break;
				default:
					echo "You enter a invalid choice";
					break;
			}
		}
	}
?>
index.php
<?php
	require("SwitchOop.php");
	$obj=new SwitchOop();
	echo $obj->switchInitialize(3,5,3);
?>
banner

Operators in PHP

Operator:-      an operator is a symbol that manipulates one or more values, usually producing a new value in the process. Meanwhile, an expression in PHP is anything that evaluates to a value; this can be any combination of values, variables, operators, and functions. In the preceding example, $x + $y is an expression. Here are some more examples of expressions:
$x + $y + $z
$x – $y
$x
5
true
gettype( $test_var )
The values and variables that are used with an operator are known as operands .
banner

Data Types In PHP

PHP Data Types

There are three types of data types present in PHP. Thats are;

1. Scalar Data Type

2. Compound Data Type

3. Special Data Type

Scalar Data Type

Scalar data means which contains only a single value. PHP support four types of scalar data types. 

i) Integer :- represent a whole number (positive or negative) (e.g. 12,15,-8,7 etc)

ii) Float :- represent a fractional number (e.g. 12.7,8.67,9.542 etc)

iii) String :- A series of characters either within ” ” or ‘ ‘ (e.g. “Abhishek” , ‘bongcode’)

iv) Boolean :- It represent either true or false 

Compound Data Type

Compound data means that contains more than one values.  PHP support two types of compound data types.

i) Array :- It is a collection of homogeneous data types . Which accessed using one name. 

ii) Object :-   A type that contain properties and methods.

                                   Special Data Type

PHP supports two special data types. They are special because they don’t contain scalar or compound but have specific meaning.

i) Resource :- contains a refrence to an external resource. (e.g. file)

ii)NULL :- That don’t contain any value.

banner

Using Variables In PHP

Definition:-    A variable is simply a container that holds a certain value. Variables get their name because that certain value can change throughout the execution of the script. It ’ s this ability to contain changing values that make variables so useful.
Code:- 
     <?php
        $x=4;//1st variable is x
        $y=8;//2nd variable is y
        echo $x+$y;//here we add two variables x and y
     ?>
Naming Variables:-
     A variable consists of two parts: the variable ’ s name and the variable ’ s value. Because you ’ ll be using
     variables in your code frequently, it ’ s best to give your variables names you can understand and
     remember. Like other programming languages, PHP has certain rules you must follow when naming
     your variables:
     i)  Variable names begin with a dollar sign ( $ )
     ii) The first character after the dollar sign must be a letter or an underscore
     iii) The remaining characters in the name may be letters, numbers, or underscores without a fixed limit
     for example; $x,$rb_web,$rbwebsolutions,$_123 etc