Category: Web Development

using a list data structure in php

Sometimes it is handy to use “list” in php. Here is the code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php function small_numbers() { $myarray = array(0,1,2); return $myarray; // return array(0,1,2); } list($num1, $num2, $num3) = small_numbers(); echo $num1 . " " . $num2 . " " . $num3 . "<br>";

uploading multiple files in php

To upload multiple files in php, we can use the following code. You need to set the path of upload directory $uploaddir in the code <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php if( isset($_POST['btnsubmit']) ) { //print_r($_FILES); $uploaddir = 'A:/AAA/www/web/uploads/'; foreach($_FILES["userfile"]["error"] as $key => $error) { if( $error == UPLOAD_ERR_OK) {

uploading single file in php

To upload a single file, the following code is useful. NOTE: you just need to fix the path of $uploaddir <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php if( isset($_POST['btnsubmit']) ) { //print_r($_FILES); $uploaddir = 'A:/AAA/www/web/uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully

nested if else in php

Nested if else in php has slightly different syntax from c++. Here is an example: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php $a=10; $b=20; if($a > $b) { echo "a is greater than b"; } elseif($a == $b) { echo "a is equal to b"; } else { echo "a is

goto statement in php

This is how we can use goto statement php: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php goto a; echo 'Hello'; a: echo 'World'; ?> </body> </html>  

loops and break statement in php

In this example, we see how we can use loops and break statements in php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php $num=10; while($num >= 0) { $num--; for($i=0; $i<10; $i++) { echo "loop i = " . $i . " <br>" ; if($num == 5) { echo "<br>Loop is break<br>";

object oriented Interfaces in PHP

Like other object oriented programming languages, we can define Interfaces in php as well. Here is the code to define and use interfaces in php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php interface MyInterface { } class MyClass implements MyInterface { } $a = new MyClass; $b = new MyClass; $c

object oriented Inheritance in php

We can use Objected Oriented Programming model in PHP which has rich set of features. Here an example of inheritance in PHP is presented: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php class ParentClass { function ShowInParent() { echo "This is in Parent Class<br>"; } } class MyClass extends ParentClass { function

Creating a dynamic html table in php

With PHP we can dynamically create HTML table and fetch record from database to show into that table. In the following form, user is asked to input to numbers. index.php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <form id="frm" name="frm" method="post" action="table.php" > <table border="1"> <tr> <td>Enter Number</td> <td> <input id="txtnum" name="txtnum"

update multi-value attribute record in php

Sometimes, we need to update a  record in php that has multi-value attributes. At database end, the multi-value attributes are stored in a separate table, with the primary of the record against whose the multi-value attributes are stored. In php, to update multi-value attributes, we need to do some extra work. Suppose we have file: