Programming Tips & Insights

MySQL: Add Statistics Column for the Number Count of Records from Another Table

It’s hard to describe this scenario precisely with limited title length. Basically, this is when you want to dedicate a field in ‘category‘ table to store the number of articles or posts in ‘post‘ table that fall into this category. Previously, you had both ‘category’ table to store post categories and ‘post’ table to store …

MySQL: Add Statistics Column for the Number Count of Records from Another Table Read More »

PHP: Convert between Numerical Bases | Binary Number, Decimal Number, Octal Number and Hexadecimal Number Conversions

Different number notations for different numerical bases: 0144 // base 8 100 // base 10 0x64 // base 16 To convert between number bases, use PHP base_convert() function: $hex = ‘a1’; // hexadecimal number (base 16) $decimal = base_convert($hex, 16, 10); // $hex converted from base 16 to base 10 and $decimal is now 161 …

PHP: Convert between Numerical Bases | Binary Number, Decimal Number, Octal Number and Hexadecimal Number Conversions Read More »

PHP: Convert Degrees to Radians and Convert Radians to Degrees

Degrees and radians are different metric means to gauge an angle. They are mutually convertible in PHP. Simply use the PHP degrees to radians function deg2rad() and PHP radians to degrees function rad2deg(). $degrees = 180; $radians = deg2rad($degrees); // 3.1416 $degrees = rad2deg($radians); // 180 Because trigonometric calculations are usually done in radians, you …

PHP: Convert Degrees to Radians and Convert Radians to Degrees Read More »

PHP: Number Format Function to Format Numbers in PHP (Integer or Float)

To display numbers to end users, especially large numbers, you may need to format them to be more user friendly. For example, with a large integer 43386000.75, it is usually preferred to comma-separate every 3 digits to this: 43,386,000.75 To format numbers this way in PHP, you need number_format() function: print number_format(43386000.75); // prints ‘43,386,000.75’ …

PHP: Number Format Function to Format Numbers in PHP (Integer or Float) Read More »

PHP: Send HTML Email (mail)

As we all know the simplest approach to create an email message and send it out is to use the php mail() function. A typical usage example would be: mail(‘[email protected]’, ‘Subject Title’, ‘Message body goes here.’); However, as the mail() function sends emails in text/plain mime type by default, if you include HTML code in …

PHP: Send HTML Email (mail) Read More »

PHP: Wrap Long Lines | Insert Line Breaks into a Long String

Some strings of texts may lack necessary wraps and don’t have natural line breaks in them so that the lines are too long to read comfortably. Therefore, you want a way to insert line breaks in to the string every dozens of characters. The php function you need is wordwrap(): $s = ‘ … ‘; …

PHP: Wrap Long Lines | Insert Line Breaks into a Long String Read More »

PHP String Case: Uppercase all Letters or Lowercase all Letters in a String | Uppercase First Letter of a String | Uppercase First Letter of all Words in a String

It’s common that you need to reformat the texts in PHP and make them look a little more formal. For example, the first letter of all sentences should be uppercased, or with a title, the first letter of all words should also be made uppercase; in other situations, you may want all letters to be …

PHP String Case: Uppercase all Letters or Lowercase all Letters in a String | Uppercase First Letter of a String | Uppercase First Letter of all Words in a String Read More »

PHP: Change Tabs to Spaces and Vice Versa in String | Change Tab Sizes / Lengths in Strings

Everyone who ever dealt with formatting code in PHP string manipulations may want to switch between different tab sizes or simply change spaces to tabs or tabs to spaces in the formatted code string. The function we’ll need for these is str_replace(), the string search and replace php function. For example: Switch between tabs and …

PHP: Change Tabs to Spaces and Vice Versa in String | Change Tab Sizes / Lengths in Strings Read More »

PHP: Check if a string contains another string or substring

As a web site templating language, string processing is what PHP is good at. There’s always a chance that you need to verify and make sure if a string contains another string. For example, to check if an at sign is included in the entered email address by a visitor: if (strpos($_POST[’email’], ‘@’) !== false) …

PHP: Check if a string contains another string or substring Read More »

PHP: How to access a global variable inside a function without passing it?

Ideally, global variables outside a function can not and should not be accessed from within the function to hold up the programming modularization paradigm. However, if you really need to do this, especially in small programs, you can declare the variable again inside the function by the keyword ‘global‘ so as to use inside the …

PHP: How to access a global variable inside a function without passing it? Read More »

PHP cURL: Fetching URL and Sending Request with Cookies

One of the things the remote web server inspects is the client cookie to know about the requester. If you need cURL to simulate a user browser that sends cookie information to the web server, you need the following options: $c = curl_init(‘http://www.example.com/needs-cookies.php’); curl_setopt ($c, CURLOPT_COOKIE, ‘user=ellen; activity=swimming’); curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); $page = curl_exec …

PHP cURL: Fetching URL and Sending Request with Cookies Read More »

PHP cURL: Making POST Request to URL

By default, all HTTP requests made out by cURL is GET requests that simply fetches the URL and don’t submit any more POST variables. However, if you need to fetch and retrieve URL by the POST method with cURL, you need the snippet below: $url = ‘http://www.example.com/submit.php’; // The submitted form data, encoded as query-string-style …

PHP cURL: Making POST Request to URL Read More »

PHP: GD Library Drawing Functions Reference

To draw a line, use ImageLine( ): ImageLine($image, $x1, $y1, $x2, $y2, $color); To draw an open rectangle, use ImageRectangle( ): ImageRectangle($image, $x1, $y1, $x2, $y2, $color); To draw a solid rectangle, use ImageFilledRectangle( ): ImageFilledRectangle($image, $x1, $y1, $x2, $y2, $color); To draw an open polygon, use ImagePolygon( ): $points = array($x1, $y1, $x2, $y2, …

PHP: GD Library Drawing Functions Reference Read More »

PHP: Change Error Reporting Level | Different PHP Error Types

Debugging a PHP script, you may want to alter the error-displaying sensitivity on a particular page and control what types of errors should be reported. The solution is the PHP error reporting function error_reporting(): error_reporting(E_ALL); // everything error_reporting(E_ERROR | E_PARSE); // only major problems error_reporting(E_ALL & ~E_NOTICE); // everything but notices As the parameters of …

PHP: Change Error Reporting Level | Different PHP Error Types Read More »

PHP: Randomizing All Lines of a File – Shuffle Lines in a Text File

Of course, you will have to read in the file first, preferably in an array. To read a file in an array, you just need the file() function: $lines = file(‘quotes.txt’); Then, you shuffle the array with shuffle() function that randomizes all the items in the array thus shuffling the lines in the file quotes.txt: …

PHP: Randomizing All Lines of a File – Shuffle Lines in a Text File Read More »

Scroll to Top