PHP#
Basic use of operators#
<?php
echo "Hello World - Printed using php";
// dynamically allocated variables
$variable1 = 4;
$variable2 = 5;
echo $variable1;
echo $variable2;
echo $variable1 + $variable2;
Echo $variable1 + $variable2;
// Operators in PHP
// Arithmrtic Operators
echo "<h1>Arithmetic Operators</h1>";
echo "Addition <br>";
echo $variable1 + $variable2;
echo "Sybtraction <br>";
echo $variable1 - $variable2;
echo "Multiplication <br>";
echo $variable1 * $variable2;
echo "Division <br>";
echo $variable1 / $variable2;
echo "<br>";
// Assignment Operators
echo "<h1>Assignment Operators</h1>";
echo "The first variable was ";
echo $variable1;
echo "<br>";
echo "The second variable was ";
echo $variable2;
echo "<br>";
$newvariable=$variable1;
echo "The new variable is ";
echo $newvariable;
echo "<br>";
// $newvariable /= 2;
// $newvariable *= 2;
$newvariable *= 2;
echo $newvariable;
// Comparison Operators
echo "<h1> Comparison Operators</h1>";
echo "<br>";
echo "The value of 1 == 4 is ";
echo var_dump(1 == 4);
echo "<br>";
echo "The value of 1 != 4 is ";
echo var_dump(1 != 4);
echo "<br>";
echo "The value of 1 >= 4 is ";
echo var_dump(1 >= 4);
echo "<br>";
echo "The value of 1 <= 4 is ";
echo var_dump(1 <= 4);
echo "<br>";
// Increment/Decrement Operators
echo "<h1> Increment/Decrement Operators </h1> <br> ";
$new_variable = 5;
echo "The variable is ";
echo $new_variable;
echo "<br>";
++$new_variable;
echo $new_variable;
echo "<br>";
--$new_variable;
--$new_variable;
echo $new_variable;
echo "<br>";
$new_variable++;
$new_variable++;
echo $new_variable;
echo "<br>";
$new_variable--;
$new_variable--;
echo $new_variable;
echo "<br>";
// Logical Operators
// and (&&)
// or (||)
// xor (^)
// not (!)
echo "<h1> Logical Operators </h1>";
echo var_dump(true && true);
echo var_dump(true and false);
echo "<br>";
echo var_dump(true || false);
echo var_dump(true or true);
echo "<br>";
$my_var = 2^4;
echo $my_var;
echo var_dump(2 xor 6 );
?>
Datatypes#
<?php
define("PI" , 3.14);
// Datatypes in php
// 1. String
// 2. Integer
// 3. Float
// 4. Boolean
// 5. Array
// 6. Object
echo "<h1> DATA - TYPES </h1>";
$new_string = "This is a string";
echo "<br>";
echo var_dump($new_string);
echo '<br>';
$new_string = 67;
echo "<br>";
echo var_dump($new_string);
echo '<br>';
$new_string = 67.23;
echo "<br>";
echo var_dump($new_string);
echo '<br>';
$new_string = true;
echo "<br>";
echo var_dump($new_string);
// constants
// defined on top;
echo "<br>";
echo PI;
echo "<br>";
?>
Basic use of Arrays and loops#
<?php
echo "<h1> If else </h1>". "<br>";
$age = 6;
if($age>18){
echo "You can go to the party";
}
else if($age==7){
echo "You are 7 years old";
}
else if($age==6){
echo "You are 6 years old";
}
else{
echo "You can not go to the party";
}
echo "<h1>ARRAYS</h1>"."<br>";
// Arrays in php
$languages = array("Python", "C++", "php", "NodeJs");
// echo count($languages);
// echo $languages[0];
echo "<h1>LOOPS</h1>"."<br>";
// Loops in PHP
$a = 0;
while ($a <= 10) {
echo "<br>The value of a is: ";
echo $a;
$a++;
}
// Iterating arrays in PHP using while loop
$a = 0;
while ($a < count($languages)) {
echo "<br>The value of language is: ";
echo $languages[$a];
$a++;
}
echo "<h1>WHILE LOOP</h1>"."<br>";
// Do while loop
$a = 200;
do {
echo "<br>The value of a is: ";
echo $a;
$a++;
} while ($a < 10);
echo "<h1>USING FOR LOOP</h1>"."<br>";
// For loop
for ($a=60; $a < 10; $a++) {
echo "<br>The value of a from the for loop is: ";
echo $a;
}
echo "<h1>USING FOREACH</h1>"."<br>";
foreach ($languages as $value) {
echo "<br>The value from foreach loop is ";
echo $value;
}
echo "<h1>USING FUNCTIONS</h1>"."<br>";
function print5(){
echo "FIVE";
}
print5();
print5();
print5();
print5();
function print_number($number){
echo "<br>Your number is ";
echo $number;
}
print_number(45);
print_number(435);
print_number(5);
?>
Strings#
<?php
$str = "This this this";
echo "<h1>CONCATENING TWO STRINGS USING .</h1>"."<br>";
echo $str. "<br>";
echo "<h1>CALCULATING THE LENGTH OF THE STRING </h1>"."<br>";
$lenn = strlen($str);
echo "The length of this string is ". $lenn . ". Thank you <br>";
echo "<h1>CALCULATING THE NUMBER OF WORDS IN A STRING</h1>"."<br>";
echo "The number of words in this string is ". str_word_count($str) . ". Thank you <br>";
echo "<h1>REVERSING THE STRING</h1>"."<br>";
echo "The reversed string is ". strrev($str) . ". Thank you <br>";
echo "<h1>FINDING A SUBSTRING IN A STRING</h1>"."<br>";
echo "The search for is in this string is ". strpos($str, "is") . ". Thank you <br>";
echo "<h1>REPLACING ONE SUBSTRING OF STRING WITH ANOTHER</h1>"."<br>";
echo "The replaced string is ". str_replace("is", "at", $str) . ". Thank you <br>";
// echo $lenn;
?>