PHP Basics and Operators Programs

1. Declare variables for name, age, and city and display them

				
					<?php
// Declaring variables
$name = "Boby Kumar";
$age = 25;
$city = "New Delhi";

// Displaying values
echo "<h3>User Information</h3>";
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "City: " . $city . "<br>";
?>

				
			

2. Create constants for PI and SITE_NAME and print them

				
					<?php
// Defining constants
define("PI", 3.14159);
define("SITE_NAME", "GetMeSkilled");

// Displaying constants
echo "<h3>Constant Values</h3>";
echo "Value of PI: " . PI . "<br>";
echo "Website Name: " . SITE_NAME . "<br>";
?>

				
			

3. Add two numbers using variables

				
					<?php
$num1 = 20;
$num2 = 15;

// Performing addition
$sum = $num1 + $num2;

echo "<h3>Addition Program</h3>";
echo "First Number: $num1 <br>";
echo "Second Number: $num2 <br>";
echo "Sum: $sum";
?>

				
			

4. Demonstrate integer, float, string, boolean data types

				
					<?php
$integerValue = 100;
$floatValue = 10.75;
$stringValue = "PHP Programming";
$booleanValue = true;

echo "<h3>PHP Data Types</h3>";

echo "Integer Value: $integerValue <br>";
echo "Float Value: $floatValue <br>";
echo "String Value: $stringValue <br>";
echo "Boolean Value: ";
var_dump($booleanValue);
?>

				
			

5. Swap two numbers using a temporary variable

				
					<?php
$a = 10;
$b = 20;

echo "<h3>Before Swapping</h3>";
echo "A = $a <br>";
echo "B = $b <br>";

// Using temporary variable
$temp = $a;
$a = $b;
$b = $temp;

echo "<h3>After Swapping</h3>";
echo "A = $a <br>";
echo "B = $b <br>";
?>

				
			

6. Swap two numbers without using a temporary variable

				
					<?php
$a = 15;
$b = 25;

echo "<h3>Before Swapping</h3>";
echo "A = $a <br>";
echo "B = $b <br>";

// Swapping without temporary variable
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;

echo "<h3>After Swapping</h3>";
echo "A = $a <br>";
echo "B = $b <br>";
?>

				
			

7. Check type of a variable using gettype()

				
					<?php
$value = 45.8;

echo "<h3>Variable Type Checking</h3>";
echo "Value: $value <br>";
echo "Type of Variable: " . gettype($value);
?>

				
			

8. Demonstrate arithmetic operators

				
					<?php
$a = 20;
$b = 5;

echo "<h3>Arithmetic Operators</h3>";

echo "Addition: " . ($a + $b) . "<br>";
echo "Subtraction: " . ($a - $b) . "<br>";
echo "Multiplication: " . ($a * $b) . "<br>";
echo "Division: " . ($a / $b) . "<br>";
echo "Modulus: " . ($a % $b) . "<br>";
?>

				
			

9. Compare two numbers using comparison operators

				
					<?php
$num1 = 30;
$num2 = 40;

echo "<h3>Comparison Operators</h3>";

echo "Num1 == Num2 : ";
var_dump($num1 == $num2);

echo "<br>Num1 > Num2 : ";
var_dump($num1 > $num2);

echo "<br>Num1 < Num2 : ";
var_dump($num1 < $num2);
?>

				
			

10. Demonstrate logical operators

				
					<?php
$a = true;
$b = false;

echo "<h3>Logical Operators</h3>";

echo "A AND B: ";
var_dump($a && $b);

echo "<br>A OR B: ";
var_dump($a || $b);

echo "<br>NOT A: ";
var_dump(!$a);
?>

				
			

11. Calculate area of a rectangle

				
					<?php
$length = 12;
$width = 6;

$area = $length * $width;

echo "<h3>Area of Rectangle</h3>";
echo "Length: $length <br>";
echo "Width: $width <br>";
echo "Area: $area";
?>

				
			

12. Calculate area of a circle

				
					<?php
$radius = 7;
$pi = 3.1416;

$area = $pi * $radius * $radius;

echo "<h3>Area of Circle</h3>";
echo "Radius: $radius <br>";
echo "Area: $area";
?>

				
			

13. Convert Celsius to Fahrenheit

				
					<?php
$celsius = 30;

$fahrenheit = ($celsius * 9/5) + 32;

echo "<h3>Temperature Conversion</h3>";
echo "Celsius: $celsius °C <br>";
echo "Fahrenheit: $fahrenheit °F";
?>

				
			

14. Increment and decrement a number

				
					<?php
$number = 10;

echo "<h3>Increment and Decrement</h3>";

echo "Original Number: $number <br>";

$number++;
echo "After Increment: $number <br>";

$number--;
echo "After Decrement: $number <br>";
?>

				
			

15. Demonstrate assignment operators

				
					<?php
$value = 20;

echo "<h3>Assignment Operators</h3>";

$value += 10;
echo "After += 10: $value <br>";

$value -= 5;
echo "After -= 5: $value <br>";

$value *= 2;
echo "After *= 2: $value <br>";
?>

				
			

16. Demonstrate concatenation operator

				
					<?php
$firstName = "Boby";
$lastName = "Kumar";

$fullName = $firstName . " " . $lastName;

echo "<h3>Concatenation Example</h3>";
echo "Full Name: " . $fullName;
?>

				
			

17. Display length of a string

				
					<?php
$text = "Welcome to PHP Programming";

echo "<h3>String Length</h3>";
echo "Text: $text <br>";
echo "Length of String: " . strlen($text);
?>

				
			

18. Check if a variable is numeric

				
					<?php
$value = 100;

echo "<h3>Numeric Check</h3>";

if(is_numeric($value)){
    echo "$value is a numeric value";
}else{
    echo "$value is NOT numeric";
}
?>

				
			

19. Calculate simple interest

				
					<?php
$principal = 5000;
$rate = 5;
$time = 2;

$simpleInterest = ($principal * $rate * $time) / 100;

echo "<h3>Simple Interest Calculation</h3>";
echo "Principal: $principal <br>";
echo "Rate: $rate % <br>";
echo "Time: $time years <br>";
echo "Simple Interest: $simpleInterest";
?>

				
			

20. Calculate average of three numbers

				
					<?php
$num1 = 40;
$num2 = 50;
$num3 = 60;

$average = ($num1 + $num2 + $num3) / 3;

echo "<h3>Average Calculation</h3>";

echo "Number 1: $num1 <br>";
echo "Number 2: $num2 <br>";
echo "Number 3: $num3 <br>";

echo "Average: $average";
?>