0% found this document useful (0 votes)
4 views3 pages

PHP Programs Output

The document contains PHP code snippets demonstrating various programming concepts including server info, variable scope, number manipulation, prime numbers, factorial, Fibonacci sequence, numeric and string functions, matrix operations, associative arrays, and date/time functions. Each section provides a brief output description of the results generated by the code. Overall, it serves as a practical guide for PHP programming examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

PHP Programs Output

The document contains PHP code snippets demonstrating various programming concepts including server info, variable scope, number manipulation, prime numbers, factorial, Fibonacci sequence, numeric and string functions, matrix operations, associative arrays, and date/time functions. Each section provides a brief output description of the results generated by the code. Overall, it serves as a practical guide for PHP programming examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

PHP Server Info


<?php
phpinfo();
?>
Output: Output: A page showing all PHP server details (version, extensions, environment).

2. Variable Scope
<?php
$globalVar = 10;

function testScope() {
$localVar = 20; // local
static $staticVar = 0; // static

global $globalVar; // using global

$staticVar++;
echo "Local Variable: $localVar<br>";
echo "Global Variable: $globalVar<br>";
echo "Static Variable: $staticVar<br><br>";
}

testScope();
testScope();
testScope();
?>
Output: Output: Local Variable: 20 Global Variable: 10 Static Variable: 1 ... Static Variable
increases each call.

3. Reverse a Number and Sum


<?php
$num = 1234;
$rev = strrev($num);
$sum = array_sum(str_split($num));
echo "Original Number: $num<br>";
echo "Reversed Number: $rev<br>";
echo "Sum of Digits: $sum<br>";
?>
Output: Output: Original Number: 1234 Reversed Number: 4321 Sum of Digits: 10

4. First 20 Prime Numbers


<?php
$count = 0; $num = 2;
while ($count < 20) {
$prime = true;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) { $prime = false; break; }
}
if ($prime) {
echo $num . " ";
$count++;
}
$num++;
}
?>
Output: Output: 2 3 5 7 11 13 17 ... 71

5. Factorial
<?php
$num = 5;
$fact = 1;
for ($i = 1; $i <= $num; $i++) { $fact *= $i; }
echo "Factorial of $num is $fact";
?>
Output: Output: Factorial of 5 is 120

6. Fibonacci (Recursive)
<?php
function fib($n) {
if ($n == 0) return 0;
elseif ($n == 1) return 1;
else return fib($n-1) + fib($n-2);
}
for ($i = 0; $i < 20; $i++) { echo fib($i) . " "; }
?>
Output: Output: 0 1 1 2 3 5 8 13 ... 4181

7. Numeric Functions
<?php
echo "Abs: " . abs(-5) . "<br>";
echo "Round: " . round(3.6) . "<br>";
echo "Ceil: " . ceil(3.2) . "<br>";
echo "Floor: " . floor(3.8) . "<br>";
echo "Pow: " . pow(2,3) . "<br>";
echo "Sqrt: " . sqrt(16) . "<br>";
echo "Rand: " . rand(1,10) . "<br>";
?>
Output: Output: Numeric operations with sample results.

8. String Functions
<?php
$str = "Hello World";
echo "Length: " . strlen($str) . "<br>";
echo "Word Count: " . str_word_count($str) . "<br>";
echo "Reverse: " . strrev($str) . "<br>";
echo "Upper: " . strtoupper($str) . "<br>";
echo "Lower: " . strtolower($str) . "<br>";
echo "Replace: " . str_replace("World", "PHP", $str) . "<br>";
echo "Substring: " . substr($str, 0, 5) . "<br>";
?>
Output: Output: Shows string operations like length, reverse, replace.

9. Matrix Addition
<?php
$a = [[1,2],[3,4]];
$b = [[5,6],[7,8]];
$result = [];
for ($i=0; $i<2; $i++) {
for ($j=0; $j<2; $j++) {
$result[$i][$j] = $a[$i][$j] + $b[$i][$j];
}
}
foreach ($result as $row) { echo implode(" ", $row) . "<br>"; }
?>
Output: Output: 6 8 10 12

10. Matrix Multiplication


<?php
$a = [[1,2],[3,4]];
$b = [[5,6],[7,8]];
$result = [];
for ($i=0; $i<2; $i++) {
for ($j=0; $j<2; $j++) {
$result[$i][$j] = 0;
for ($k=0; $k<2; $k++) {
$result[$i][$j] += $a[$i][$k] * $b[$k][$j];
}
}
}
foreach ($result as $row) { echo implode(" ", $row) . "<br>"; }
?>
Output: Output: 19 22 43 50

11. Associative Array Ascending (by value)


<?php
$marks = ["Krishna"=>90, "Aman"=>75, "Neha"=>85];
asort($marks);
print_r($marks);
?>
Output: Output: Array ( [Aman] => 75 [Neha] => 85 [Krishna] => 90 )

12. Associative Array Descending (by key)


<?php
$marks = ["Krishna"=>90, "Aman"=>75, "Neha"=>85];
krsort($marks);
print_r($marks);
?>
Output: Output: Array ( [Neha] => 85 [Krishna] => 90 [Aman] => 75 )

13. Date and Time Functions


<?php
echo "Current Date: " . date("Y-m-d") . "<br>";
echo "Current Time: " . date("h:i:s A") . "<br>";
echo "Day: " . date("l") . "<br>";
echo "Timestamp: " . time() . "<br>";
?>
Output: Output: Shows current date, time, day, and Unix timestamp.

You might also like