Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

PHP Programming

The document provides a comprehensive overview of PHP programming basics, covering topics such as writing sample programs, declaring variables, arrays, string operations, type casting, conditional statements, loops, functions, and exception handling. It also includes sections on form handling, file handling, sessions, and cookies in PHP. Each section contains code examples demonstrating the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PHP Programming

The document provides a comprehensive overview of PHP programming basics, covering topics such as writing sample programs, declaring variables, arrays, string operations, type casting, conditional statements, loops, functions, and exception handling. It also includes sections on form handling, file handling, sessions, and cookies in PHP. Each section contains code examples demonstrating the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PHP Programming Basics

1. Write a Sample Program in PHP


<! DOCTYPE html>
<html>
<body>

<?php
echo "Hello, Welcome to PHP Program</br>";
echo "Computer Engineering, ADI"
?>

</body>
</html>

2. Declare Variables in PHP


<! DOCTYPE html>
<html>
<body>

<?php
$a = "ADIT";
$b =’c’;
$x = 14;
$y = 102.55;
$z = $x+$y

echo "Hello " .$a. "Welcome";


echo "Total is ". $z;

</body>
</html>
3. Declaring Array in PHP
<! DOCTYPE html>
<html>
<body>
<?php

$branch = array("Computer","Electronics","Mechanical","Food Processing");


echo $cars[0];

?>

</body>
</html>

4. String operations in PHP


<! DOCTYPE html>
<html>
<body>
<?php

$a = "Hello everyone, welcome to ADIT";


echo strlen($a);
echo str_word_count($a);
echo strrev($a);
echo strpos($a, "ADIT");
echo str_replace("Hello", "Hi", $a);

?>

</body>
</html>
5. Type casting in PHP
<?php

$x = 23465.768; // Cast float to int


$y = (int)$x;
echo $y;

$x = "23465.768"; // Cast string to int


$y = (int)$x;
echo $y;
?>

6. If conditions in PHP
<?php

$x = 10

if ($x >+ 0) {
echo "The number is positive";
} else {
echo "The number is negative";
}

$dept = "IT";

switch ($dept) {
case "IT":
echo "Welcome to Information Technology";
break;
case "CP":
echo "Welcome to Computer Engineering";
break;
case "EC":
echo "Welcome to Electronics & Communication";
break;
default:
echo "Welcome to ADIT";
}

?>

7. Loops in PHP
//While loop--------------------------------------------
<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
//For Loop---------------------------------------------------
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
//Do while loop--------------------------------------------------
<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
8. Functions in PHP
<?php
function welcome() {
echo "Welcome to ADIT";
}
Welcome();

function add(int $a, int $b) {


return $a + $b;
}
echo add(5, 10);

function branch_name($branch) {
echo "Welcome to ". $branch. " of ADIT,</br>";
}
branch_name("CP")
?>

9. Exceptions in PHP
<?php
function divide($a, $b) {
if($a == 0) {
throw new Exception("Division by zero");
}
return $a / $b;
}

try {
echo divide(5, 0);
} catch(Exception $e) {
echo "Unable to divide. ";
} finally {
echo "Process complete.";
}
?>
PHP Form Handling

1. Form Page
<html>
<body>

<form action="registration.php" method="post">


Student Name: <input type="text" name="name"><br>
Enrolment Number: <input type="text" name="enrol"><br>
Branch: <input type="text" name="branch"><br>
<input type="submit">
</form>

</body>
</html>

2. Background Processing:

Page: Registration.php

<html>
<body>

<h2>The details of the student are</h2><br>


Name: <?php echo $_POST["name"]; ?> <br>
Enrolment Number: <?php echo $_POST["enrol"]; ?> <br>
Branch: <?php echo $_POST["branch"]

</body>
</html>
File Handling in PHP
1. Open/Close existing file
<?php
$myfile = fopen("Aditdata.txt", "r") or die("Error");
echo fread($myfile,filesize("aditdata.txt"));
fclose($myfile);
?>

2. Writing into a file


<?php
$txt = "Hello students, welcome to ADIT\n";
$myfile = fopen("aditdata.txt", "w") or die("Error");
fwrite($myfile, $txt);
fclose($myfile);

?>
Sessions & Cookies
1. Sessions
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php

$_SESSION["col"] = "ADIT";
$_SESSION["branch"] = "CP";
?>

//Checking session variables


<?php
// Echo session variables that were set on previous page
echo "Welcome, your college is " . $_SESSION["col"] . ".<br>";
echo "Your Branch is " . $_SESSION["branch"] . ".";
?>
</body>
</html>

2. Cookies
<?php
$name = "Col";
$ value = "ADIT";
setcookie($name, $value, time() + (86400 * 30), "/");
?>
<?php
echo "The parameter is " . $name . "<br>";
echo "Your collge is " . $_COOKIE[$name];
}
?>

<html>
<body>

You might also like