- PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.
- Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Group.
- PHP originally stood for Personal Home Page, but it now stands for the recursive backronym PHP: Hypertext Preprocessor.
- PHP code may be embedded into HTML code, or it can be used in combination with various web template systems, web content management systems and web frameworks.
- PHP code is usually processed by a PHP interpreter implemented as a module in the web server or as a Common Gateway Interface (CGI) executable.
- The web server combines the results of the interpreted and executed PHP code, which may be any type of data, including images, with the generated web page.
<?php
// to print anything on browser
echo "Hello World";
#--------------------------------------------------------------------------------------#
/* :: variables ::
-> to store data
-> variable name must start with $
-> after $ ,start with _ or any letter (not number)
-> case sensitive , no space
*/
$first = 5;
$First = 10;
$_fname = "Devansh";
$_lname = "Amdavadwala";
echo "<br>$first";
echo "<br>$First";
echo "<br>$_fname";
echo "<br>".$_fname.$_lname;
#--------------------------------------------------------------------------------------#
/* :: Operators ::
-> Arithmatic : { + , - , * , / , % , ++ , -- }
-> Logical : { && , || }
-> Ternary : <condition> ? <true> : <false>;
-> Assignment : { = , += , -= , *= , /= , %= }
-> Comparison : { == , != , > , < , >= , <= }
*/
#--------------------------------------------------------------------------------------#
/* :: Conditional Statement ::
if ( condition 1){
} elseif ( condition 2) {
} else {
}
*/
/* :: switch ::
switch (variable) {
case 'value':
// code...
break;
default:
// code...
break;
}
*/
#--------------------------------------------------------------------------------------#
/* :: Array ::
-> array() : to create array
i) index array
ii) Associatative array
iii) Multidimensional array
*/
// index array
$arr1 = array("Car1","Car2","Car3");
$arr1[] = "Car4"; // to add value at last index
$arr1[2] = "Car5"; // to update value at index
echo "<br><pre>";
print_r($arr1);
echo "</pre>";
// Associatative array
$arr2 = array(
'name' => 'Devansh',
'company' => 'Creative',
'age' => 20,
'status' => true,
);
$arr2['test'] = 'testing'; // to add value
echo "<br><pre>";
print_r($arr2);
echo "</pre>";
// Multidimensional array
$arr3 = array(
'name' => 'Devansh',
'contact' => array('5367289098','9876543567'),
'age' => 20,
'status' => true,
);
echo "<br><pre>";
print_r($arr3);
echo "</pre>";
// another method to create array
$arrr = [1,2,3,4,5];
$arrrr = [5,6,7 => [88 ,99 ,00 ],8];
echo "<br><pre>";
print_r($arrrr);
echo "</pre>";
#--------------------------------------------------------------------------------------#
/* :: Function ::
user defined function
-> function <functionName>( argument list ){
<function body>
}
*/
// function declare/ define
function greet(){
echo " Welcome ";
}
// function call
greet();
#--------------------------------------------------------------------------------------#
/* :: Loops ::
i) while
ii) do while
iii) for
iv) foreach
*/
#--------------------------------------------------------------------------------------#
// while loop
echo "<br><br>=> while loop<br>";
$i = 0;
while ($i < 5) {
echo "$i ";
$i++;
}
#--------------------------------------------------------------------------------------#
// for loop
echo "<br><br>=> for loop<br>";
for($i = 0; $i < 5; $i++){
echo "$i ";
}
#--------------------------------------------------------------------------------------#
// do while loop
echo "<br><br>=> do while loop<br>";
$i = 0;
do{
echo "$i ";
$i++;
} while($i < 5);
#--------------------------------------------------------------------------------------#
// foreach loop
echo "<br><br>=> foreach loop";
// foreach ($variable as $key => $value) { // code... }
$arr = ["HTML" => 2500,
"CSS" => 2500,
"JS" => 3000,
"PHP" => 4000,
"LARAVEL" => 2000];
foreach ($arr as $course => $fees) {
echo "<br>The fees of $course is $fees";
}
#--------------------------------------------------------------------------------------#
// break & continue statement
/*while (condition) {
if ( some condition )
break; -> end current loop
if ( some condition )
continue; -> skip current iteration
}
*/
#--------------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------------#
/*
Notes:-
<br> - new line
. - concatinate strings
var_dump() - tell data type of variable
print_r() - to print array
<pre> </pre> - to format array
*/
?>
<?php
#--------------------------------------------------------------------------------------#
echo "<h1>Predefined Array functions </h1> ";
// array() -> to create array
$cars = array(
'A' => 'car1',
'B' => 'Car2',
'C' => 'Car3',
'D' => 'Car4',
'E' => 'Car4',
'F' => 'Car8');
#--------------------------------------------------------------------------------------#
// print_r() -> to print array
echo "=> print_r() <pre>";
print_r($cars);
echo "</pre>";
#--------------------------------------------------------------------------------------#
// is_array() -> check is it array or not
echo "=> is_array() <br>";
var_dump(is_array($cars)); // bool(true) 1
#--------------------------------------------------------------------------------------#
// array_search(needle, haystack) -> search value for that array
echo "<br><br>=> array_search()<br>";
echo array_search("Car2",$cars);
// return index of that value if found
#--------------------------------------------------------------------------------------#
// array_slice(array, offset) -> create subset of array
echo "<br><br>=> array_slice()<pre>";
print_r(array_slice($cars, 2));
echo "</pre>";
#--------------------------------------------------------------------------------------#
// array_chunk(input, size)
// -> created chunked arrays accrding to size given
// chunk -> smaller pieces
echo "<br>=> array_chunk() <pre>";
print_r(array_chunk($cars, 2));
echo "</pre>";
#--------------------------------------------------------------------------------------#
// array_pop(array) -> removes and return last element
echo "<br>=> array_pop()<br>";
echo array_pop($cars);
#--------------------------------------------------------------------------------------#
// array_push(array, var)
// -> add new element at last index
array_push($cars, "Car7");
echo "<br><br>=> array_push() <pre>";
print_r($cars);
echo "</pre>";
#--------------------------------------------------------------------------------------#
// array_keys(input) -> Create new array of all keys
echo "=> array_keys()<pre>";
print_r(array_keys($cars));
echo "</pre>";
#--------------------------------------------------------------------------------------#
// array_key_exists(key, array)
echo "=> array_key_exists()<br>";
var_dump(array_key_exists("A", $cars)); // 1 = true
echo "<br>";
var_dump(array_key_exists("H", $cars)); // 0 = false
#--------------------------------------------------------------------------------------#
// count(var) -> size of array / total values in array
echo "<br><br>=> count()<br>";
echo count($cars);
#--------------------------------------------------------------------------------------#
// array_count_values(input)
// -> count each value occurance in array
echo "<br><br>=> array_count_values()<pre>";
print_r(array_count_values($cars));
echo "</pre>";
#--------------------------------------------------------------------------------------#
// array_merge(array1) -> merge two array
$car2 = array("Car88" , "Car77", "Car55");
echo "<br>=> array_merge()<pre>";
print_r(array_merge($cars , $car2));
echo "</pre>";
#--------------------------------------------------------------------------------------#
// implode(glue, pieces) -> convert array into string
echo "=> implode()<br>";
$arr4 = ["1","2","3","4","5"];
echo implode(",", $arr4);
#--------------------------------------------------------------------------------------#
// explode(delimiter, string)
// -> separate string and convert to array
echo "<br><br>=> explode()";
$str = "A,B,C,D,E,F,G";
echo "<pre>";
print_r(explode(",", $str));
echo "</pre>";
#--------------------------------------------------------------------------------------#
/* :: String functions ::
• strtolower() - converts a string to lowercase.
• strtoupper() - converts a string to uppercase.
• lcfirst() - converts the first character of a string to lowercase.
• ucfirst() - converts the first character of a string to uppercase.
• ucwords() - converts the first character of each word in a string to uppercase.
• str_ireplace(search, replace, subject)
• strrev(string)
• strstr(haystack, needle)
• substr(string, start,end)
• str_repeat(input, multiplier)
• str_word_count(string)
• strlen(string)
*/
#--------------------------------------------------------------------------------------#
/* :: Maths functions::
• rand(start ,end); -> random number btw start and end range
• ceil(value) -> {5.2,..,5.5,..,5.8} -> 6
• floor(value) -> {5.2,..,5.5,..,5.8} -> 5
• pow(base, exp) -> base to power exp
• sqrt(arg) -> square root of arg
*/
#--------------------------------------------------------------------------------------#
// to set your time zone
date_default_timezone_set("ASIA/KOLKATA");
#--------------------------------------------------------------------------------------#
/* :: date() function modifier ::
• d - The day of the month (from 01 to 31)
• D - A textual representation of a day (three letters)
•j - The day of the month without leading zeros (1 to 31)
• l (lowercase 'L') - A full textual representation of a day
• N - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
• S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
• w - A numeric representation of the day (0 for Sunday, 6 for Saturday)
• z - The day of the year (from O through 365)
• W - The ISO-8601 week number of year (weeks starting on Monday)
• F - A full textual representation of a month (January through December)
• m - A numeric representation of a month (from 01 to 12)
• M - A short textual representation of a month (three letters)
• n - A numeric representation of a month, without leading zeros (1 to 12)
• t - The number of days in the given month
• L - Whether it's a leap year (1 if it is a leap year, 0 otherwise)
*/
echo "<br>=> getdate()<pre>";
print_r(getdate());
echo "</pre>";
echo "<br><br>=> date(format)<br>";
echo date('d-D-j-l-N-S-w-z-W-F-m-M-n-t-L');
echo "<br>".date('d-F-Y h:i:s:a');
echo "<br><br>=> time()<br>";
#--------------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------------#
?>