Displaying Content of a Text File in PHP

 
<?php
$file=’info.txt’;
$fobj=fopen($file,"r");
$text=fread($fobj, filesize($file));
echo("<h2><font color=’blue’>Displaying Content of a Text File</font><h2>");
echo("<br>");
echo("<font size=3>");
echo($text);
echo("</font>");
fclose($fobj);
?>

Creating Constructors with Classes in PHP

<?php
class student
{
var $name;
var $age;
var $grade;
function student($n,$a,$g)
{
$this->name=$n;
$this->age=$a;
$this->grade=$g;
}
function display_info()
{
echo("<p>Name [...]

Example process of creating classes and objects in PHP

<?php
class emp
{
var $name;
var $address;
var $dept;
function assign_info($n,$a,$d)
{
$this->name=$n;
$this->state=$a;
$this->dept=$d;
}
function display_info()
{
echo("<p>Employee [...]

Using Custom Error Handler in PHP

<?php
error_reporting(E_ALL);
function ErrHandler($errorno, $errorstr, $errorfile, $errorline)
{
$display = true;
$notify = false;
$halt_script = false;
$error_msg = "<br>The $errorno error is occurring at $errorline in
$errorfile<br>";
switch($errorno)
{
case E_USER_NOTICE:
[...]

Generating Errors Using the trigger_error() Function in PHP

The assert() function checks whether the divisor is zero or not.
The error_log() function sends the message, Cannot perform division by zero, to the log file specified by the second parameter when the divisor is zero.

<?php
// set the error reporting level for this script
error_reporting(E_USER_ERROR);
if (assert($divisor == 0))
{
error_log("Cannot perform division by zero", 3, "var/www/html/PHP_XML/error.log");
[...]

Using Nested Functions in PHP

<?php
function msg()
{
echo("<center><h2>Displaying even
numbers</h2></center><p><p>");
function displayeven()
{
$ctr=0;
echo("<font size=4>");
for($i=2;$i<=100;$i+=2)
{
echo("$i &nbsp;&nbsp;&nbsp;");
[...]

Example of User-Defined Function in PHP

<?php
echo("<center><h2>Displaying even numbers</h2></center><p><p>");
function displayeven()
{
$ctr=0;
echo("<font size=4>");
for($i=2;$i<=100;$i+=2)
{
echo("$i &nbsp;&nbsp;&nbsp;");
$ctr++;
if($ctr%10==0)
{
echo("<p>");
[...]

Using the for Loop in PHP

<?php
echo("Prime Numbers between 1 and 30<p>");
//For loop from 1 to 30
for($var=1;$var<30;$var++)
{
$ctr=0;
for($k=2;$k<=$var/2;$k++)
{
$d=$var%$k;
if ($d == 0)
{
$ctr=1;
[...]

Using the while Loop in PHP

<?php
$a=2;
echo("<p>Even Numbers from 2 to 30<p>");
while ($a <=30)
{
echo("$a <br>");
$a=$a+2;
}
?>

Using the switch case Conditional Statement in PHP

<?php
//Initializing variables
$a = 10;
$b = 5;
echo("1. Addition <br>");
echo("2. Subtraction <br>");
echo("3. Multiplication <br>");
echo("4. Division <br>");
$ch=3;
switch ($ch)
{
case 1:
$d=$a+$b;
echo("The sum of the two numbers is $d");
break;
case 2:
$d=$a-$b;
echo("The difference of the two numbers is $d");
[...]