How to embed images using PHPMailer


Use the AddEmbeddedImage method provided by the PHPMailer class.  eg. $mail->AddEmbeddedImage(‘test.gif’,’testImage’,’test.gif’); Parameters for AddEmbeddedImage($path, $cid, $name = ”, $encoding = ‘base64’, $type = ‘application/octet-stream’) $path = Path to the attachment $cid = Content ID of the attachment.  Use this to...

Comparison Operators


PHP comparison operators allows you to compare two values with each other. Below is a table of all the different comparison operators that are available for PHP Comparison Operators Example Name Result $a == $b Equal TRUE if $a is equal to $b. $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4) $a != $b Not equal TRUE if $a is not equal to $b. $a...

How to subtract a number of days from todays date using PHP date function


$var = ‘2007-08-15’; $var_subtracted_date = date(‘Y-m-d’, strtotime(‘-2 days’, strtotime($var))); Explanation of PHP functions: 1) FUNCTION date DESCRIPTION string date ( string $format [, int $timestamp ] ) PARAMETERS USED IN THIS EXAMPLE Y = A full numeric representation of a year, 4 digits (Examples: 1999 or 2003) m = Numeric representation of a month, with...

How to print the contents of an array using PHP


//Array example $arr = array(‘one’,’two’,’three’,’four’); Use the print_r function. Example: print_r($arr); The example above will print out the following: Array ( [0] => one [1] => two [2] => three [3] => four ) NOTE: If you want to have the contents of the array displayed in a more readable format, add a echo...

How to get the Nameservers of a domain using PHP


Use the  dns_get_record function with type DNS_NS Example: //Add this piece of code to your PHP will $result = dns_get_record(‘‘,DNS_NS); Execute the script //An array will be returned similar as below Array ( [0] => Array ( [host] => [type] => NS [target] => [class] => IN [ttl] => 70144 ) [1] => Array ( [host] =>...

What is the purpose of the & Ampersand in front of PHP Variables


What this does it creates a reference to the original variable and thus does not copy the value of the original variable. $original_var = 500; &$ref_var = $original_var; //This will print the value 500 echo  $ref_var; //Now we change the value of the original variable to 600 $original_var = 600; //Now we print the value of the reference variable, and the reference variable takes the value of...

How to embed images using PHPMailer


Use the AddEmbeddedImage method provided by the PHPMailer class. eg. $mail->AddEmbeddedImage(‘test.gif’,’testImage’,’test.gif’); Parameters for AddEmbeddedImage($path, $cid, $name = ”, $encoding = ‘base64’, $type = ‘application/octet-stream’) $path = Path to the attachment $cid = Content ID of the attachment.  Use this to...

How to solve error: ‘PHP Warning: DOMDocument::createElement(): unterminated entity reference’ when creating a new element using the DOMDocument object model


DOMDocument = Represents an entire HTML or XML document; serves as the root of the document tree.  (PHP 5) DOMDocument::createElement()  = Create new element node. (PHP 5) Now to the solution! Solution: Use the htmlentitites function to convert all applicable characters to HTML entities. $dom->createElement(‘value’, htmlentities($text_value)); htmlentitites = Convert all applicable...

How to remove values in one array from another array using PHP


Example: I have array A with values and another array B. I need to remove the values in array A from the values in array B.   Use the PHP function array_diff Description array array_diff ( array $array1 , array $array2 [, array $ … ] ) Compares array1  against array2  and returns the difference. Parameters array1 = The array to compare from array2 = An array to compare against Code...

How to remove the first element of an array using PHP and then storing the element in a variable


By using the PHP function ‘array_shift‘ we can remove the first value of an array array_shift – Shift an element off the beginning of array (PHP 4 & 5) Example: $array = array("apple","peach","pineapple","banana"); $first_element = array_shift($array); echo '<pre>'; print_r($array); echo '</pre>'; echo $first_element; In the above example we have an array with 4...

How to force a CSV file to download when clicking on a download link using PHP


Example: We have a CSV file called example.csv we want users to download to use as a template. Now some browsers will display the contents of the file instead of asking users to download the file. What we want to do is to force the downloadinstead of displaying the contents in the browser. Create a new PHP file called ‘download.php‘ Open the file for editing Copy the code below into...

How to convert all $_POST or $_GET values to Integers


By using the built-in PHP functions  array_map and intval. array_map = Applies the callback to the elements of the given arrays, this applies the function for each value of an array (PHP 4 >= 4.0.6, PHP 5) intval = Get the integer value of a variable (PHP 4, PHP 5) Examples: {codecitation style=”brush: PHP;”} $post_int = array_map(‘intval’, $_POST); //Converting every...

Script to resize PNG, JPG and GIF images using PHP keeping the aspect ratio


How this script works: What this script does, it resizes any PNG, JPG or GIF image to the given width and height and stores the image to a specified location. If the width is set to 0 it will automatically calculate the width by using the height given by keeping the aspect ratio the same is. The same goes for when the height is set to 0 and the width is given. NOTE: Make sure the GD libraries are...

Getting Warning: split(): REG_BADRPT when using the split PHP function


Getting Warning: split(): REG_BADRPT when using the split PHP function when I try to split a string after each question mark (?). E.g. $string = "ABC?DEF?BGT"; list($a,$b) = split('?',$string); Answer: The reason why this is failing is split gives you the power to use regular expressions Use the explode() function instead If you really need to use the split() function try escaping the character...

How to remove all white spaces within a string using PHP


There are two functions that could be used namely str_replace and  preg_replace. First, we’ll give an example using str_replace. str_replace is the preferred method since it’s faster and easier to use than preg_replace which relies on regular expressions. str_replace (PHP 4, PHP 5) –  $string =  "This is a test string"; $new_string = str_replace(' ','',$string); echo...

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me