search
top

MySQLi with PHP

Below is the simple PHP script to connect database using mysqli <?php $link = mysqli_connect("127.0.0.1", "username", "password", "database_name"); if (!$link) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " ....
read more

File Upload in PHP

Hello All, I had to make a custom form for file uploading in PHP, I am explaining here the way to upload a file. <?php //error_reporting(E_ALL); if(isset($_FILES) && !empty($_FILES['docname'])) { $target_dir = "../tmp/"; $target_file = $target_dir . basename($_FILES["docname"]["name"]); $isUploaded = 1; // Check if file already...
read more

MS SQL with PHP

Below is the simple PHP (>= 7.0.0) script to connect with the MS SQL database <?php try { $hostname = ""; //host name $port = 1433; //port number $dbname = ""; //database name $username = ""; //database username $pw = ""; //database password $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw"); ...
read more

Coupon Codes Generator

I am describing here a way to generate coupon codes with custom configurable parameters. <?php function re($l,$d,$pre,$suf,$charset,$del) { $length = max(1,$l); $split = max(0,$d); $suffix = $suf; $prefix = $pre; $splitChar = $del; $code = ''; $charsetSize = count($charset); for ($i=0; $i<$length; $i++) { $char = $charset[mt_rand(0,...
read more

Random String Generator

Below is a simple script to generate random string/password using PHP. You can customize the same by adding special characters. <?php function randomPassword() { $alphabet = "abcdefghijklmnopqrstuwxyz ABCDEFGHIJKLMNOPQRSTUWXYZ 0123456789"; $pass = array(); //remember to declare $pass as an array $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache ...
read more

Pyramid program in PHP

Hello All, This is kind of fun stuff. I was bored and decided to build a Pyramid Program in PHP. <?php $max = 5; //any numeric value to be assigned here for($i=1;$i<=$max;$i++) { inter($i,$max); } for($i=$max-1;$i>=1;$i--) { inter($i,$max); } function sp($j,$max) { for($i=$j;$i<$max;$i++) { echo '&nbsp;&nbsp;&nbsp;'; } } function inter($i,$max) { $j=1; sp($i,$max); while...
read more

Encode Decode image using PHP

Hello, I am describing here the way to 1) Encode image to generate a string 2) Decode image from a given encoded string 1) Here you need to specifiy the path of your image in variable ‘$image_path’ <?php $image_path = 'test_image.gif'; //this will be the physical path of your image $img_binary = fread(fopen($image_path, "r"), filesize($image_path)); ...
read more

Number of days between dates

One day I was just thinking about days spent by me on this earth, so I decided to make a script for the same. I am writing here the code, just make a new empty php file and copy and paste the script and execute it to find out the days between two dates. <h1>For finding days lived on earth</h1> <b>Select Date :</b> <form name="dob" action="<?php echo...
read more

Delete Directory in PHP

This post will be helpful to those who work with directories and files. I am writing here the PHP code for deleting the directory. <?php $extract_dir = 'path'; //physical path of the directory if(deleteDirectory($extract_dir)) //delete directory { $msg = "$extract_dir was deleted"; echo $msg."<br/>"; } else { $msg = "$extract_dir was not deleted"; echo...
read more

Passing by Reference

I am listing here a simple example to explain ‘passing by reference’ in PHP. I hope it is helpful. <?php $a = 5; echo 'A1->'.$a.'<br/>'; $b = 6; $a =& $b; echo 'A2->'.$a.'<br/>B1->'.$b.'<br/>'; global $var; function foo(&$var) { $var++; } $a=5; echo 'A3->'.$a.'<br/>'; foo($a); echo 'A4->'.$a.'<br/>Var->'.$var; ?> For...
read more

« Previous Entries

top