0% found this document useful (0 votes)
1 views

php1-23

The document contains various PHP code snippets demonstrating fundamental programming concepts such as expressions, operators, conditional statements, loops, arrays, functions, classes, and session management. It also includes examples of serialization, email validation, and basic MySQL connection syntax. Additionally, it covers object cloning, area calculation using classes, and GUI components for web pages.

Uploaded by

hasanfadnis05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

php1-23

The document contains various PHP code snippets demonstrating fundamental programming concepts such as expressions, operators, conditional statements, loops, arrays, functions, classes, and session management. It also includes examples of serialization, email validation, and basic MySQL connection syntax. Additionally, it covers object cloning, area calculation using classes, and GUI components for web pages.

Uploaded by

hasanfadnis05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

// ✅ 1.

Expressions and Operators


$a = 10;
$b = 5;
echo "Addition: ".($a + $b)."<br>";
echo "Subtraction: ".($a - $b)."<br>";
echo "Multiplication: ".($a * $b)."<br>";
echo "Division: ".($a / $b)."<br><br>";

// ✅ 2. Even or Odd
$num = 7;
echo $num." is ".($num % 2 == 0 ? "Even" : "Odd")."<br><br>";

// ✅ 3. Positive or Negative
$num = -3;
echo $num." is ".($num >= 0 ? "Positive" : "Negative")."<br><br>";

// ✅ 4. If condition with For Loop


for($i = 1; $i <= 5; $i++) {
if($i % 2 == 0) {
echo $i." is even<br>";
}
}
echo "<br>";

// ✅ 6. Multidimensional Array
$marks = array(
"Noorain" => array(90, 85, 92),
"Aisha" => array(88, 79, 84)
);
echo "Noorain's Maths Marks: ".$marks["Noorain"][0]."<br><br>";

// ✅ 7. String length and word count (manual)


$str = "PHP is powerful";
$len = 0;
foreach(str_split($str) as $char) {
$len++;
}
echo "Length: $len<br>";

$word_count = 1;
for($i = 0; $i < strlen($str); $i++) {
if($str[$i] == ' ') $word_count++;
}
echo "Words: $word_count<br><br>";

// ✅ 8. Parameterized Function
function greet($name) {
echo "Hello $name!<br>";
}
greet("Noorain");
echo "<br>";

// ✅ 9. Parameterized Constructor
class Student {
function __construct($name) {
echo "Student name is $name<br>";
}
}
$obj = new Student("Noorain");
echo "<br>";

// ✅ 10. Default Constructor


class Demo {
function __construct() {
echo "Default constructor called<br>";
}
}
$d = new Demo();
echo "<br>";

// ✅ 11. Serialization
$arr = array("apple", "banana", "cherry");
$ser = serialize($arr);
echo "Serialized: $ser<br>";

$unser = unserialize($ser);
print_r($unser);
echo "<br><br>";

// ✅ 12. Introspection
class Test {
public $a;
function show() {}
}
$obj = new Test();
echo get_class($obj)."<br>";
echo method_exists($obj, "show") ? "Method exists<br>" : "Method
doesn't exist<br>";
echo "<br>";

// ✅ 13. Email Validation


$email = "Noorain@gmail.com";
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "$email is valid<br>";
} else {
echo "$email is invalid<br>";
}
echo "<br>";

// ✅ 14. Start and Destroy Session


session_start();
$_SESSION['user'] = "Noorain";
echo "Session Started for ".$_SESSION['user']."<br>";
session_destroy();
echo "Session Destroyed<br><br>";

// ✅ 15. Update and Delete Syntax


// UPDATE table_name SET column1 = value1 WHERE condition;
// DELETE FROM table_name WHERE condition;
echo "Update & Delete syntax written<br><br>";

// ✅ 16. Factorial
$num = 5;
$fact = 1;
for($i = 1; $i <= $num; $i++) {
$fact *= $i;
}
echo "Factorial of $num is $fact<br><br>";

// ✅ 17. Connect PHP with MySQL


/*
\$conn = mysqli_connect("localhost", "root", "", "dbname");
if(\$conn) echo "Connected";
else echo "Failed";
*/
echo "MySQL connection syntax written<br><br>";

// ✅ 18. Do-while loop


$i = 1;
do {
echo "Value: \$i<br>";
$i++;
} while($i <= 5);
echo "<br>";

// ✅ 19. Update/Delete Operation Example


/*
\$sql = "UPDATE students SET name='Noorain' WHERE id=1";
\$sql = "DELETE FROM students WHERE id=1";
*/
echo "Update/Delete operations written<br><br>";

// ✅ 20. Create PDF (needs library like FPDF)


echo "PDF generation requires FPDF library<br><br>";
// ✅ 21. GUI Components Web Page (Basic Example)
echo "<form method='post'>
<input type='text' name='name'>
<input type='submit'>
</form><br><br>";

// ✅ 22. Object Cloning


class CloneDemo {
public $val = 10;
}
$obj1 = new CloneDemo();
$obj2 = clone $obj1;
echo "Cloned Object Value: ".$obj2->val."<br><br>";

// ✅ 23. Class 'Percentage'


class Percentage {
public $length;
public $width;
function area() {
return $this->length * $this->width;
}
}
$p1 = new Percentage();
$p1->length = 5;
$p1->width = 10;
echo "Area of Rectangle: ".$p1->area()."<br>";
?>

</body>
</html>

You might also like