PHP – Examples

Drawing a Circle

<?php header (“Content-type: image/png”); $im = ImageCreate (150, 150); $grey = ImageColorAllocate ($im, 230, 230, 230); $black = ImageColorAllocate ($im, 0, 0, 0); ImageString($im, 3, 5, 5, “Figure 18.5: Circle”, $black); ImageArc($im, 75, 75, 50, 50, 0, 360, $black); ImagePng ($im); ImageDestroy ($im); ?>

Email validation – Regular Expressions

   <?php    $email = “test@phpmoot.com”;    $result = ereg(“^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$”, $email, $trashed);    if($result) {     $match = “Valid”;   }    else{     $match = “Invalid”; }   echo “Email address validation about $email is ” . $match;   ?>

Adding a User into database with PHP

To insert the record into database using PHP <?php $self = $_SERVER[‘PHP_SELF’]; $firstname = $_POST[‘firstname’]; $lastname = $_POST[‘lastname’]; $username = $_POST[‘username’]; $password = $_POST[‘password’]; if((!$firstname) || (!$lastname) || (!$username) || (!$password)){ $form =”Please enter in all the fields, to a new user details…”; $form.=”<form action=\”$self\” method=\”post\”>”; $form.=”First Name: <input type=\”text\” name=\”firstname\” value=\”$firstname\”><br>”; $form.=”Last Name: <input type=\”text\” name=\”lastname\” value=\”$lastname\”><br>”; $form.=”Username:… (READ MORE)

File Upload Script

There should be the upload directory to upload the files. <html> <head> <title>A File Upload Script</title> </head> <body> <div> <?php if ( isset( $_FILES[‘fupload’] ) ) { print “name: “.     $_FILES[‘fupload’][‘name’] .”<br />”; print “size: “.     $_FILES[‘fupload’][‘size’] .” bytes<br />”; print “temp name: “.$_FILES[‘fupload’][‘tmp_name’] .”<br />”; print “type: “.     $_FILES[‘fupload’][‘type’] .”<br />”; print “error: “.    $_FILES[‘fupload’][‘error’] .”<br />”; $source = $_FILES[‘fupload’][‘tmp_name’]; $target = “upload/”.$_FILES[‘fupload’][‘name’];… (READ MORE)