php1-23
php1-23
// ✅ 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>";
// ✅ 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>";
$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>";
// ✅ 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>";
// ✅ 16. Factorial
$num = 5;
$fact = 1;
for($i = 1; $i <= $num; $i++) {
$fact *= $i;
}
echo "Factorial of $num is $fact<br><br>";
</body>
</html>