What is output buffering in PHP?

Output Buffering for Web Developers, a Beginner’s Guide: Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. Advantages of output … Read more

Pass a PHP variable to a JavaScript variable

Expanding on someone else’s answer: <script> var myvar = <?= json_encode($myVarValue, JSON_UNESCAPED_UNICODE); ?>; </script> Using json_encode() requires: PHP 5.2.0 or greater $myVarValue encoded as UTF-8 (or US-ASCII, of course) Since UTF-8 supports full Unicode, it should be safe to convert on the fly. Please note that if you use this in html attributes like onclick, … Read more

How to get a query error from prepare() in PDO PHP?

You need to set the error mode attribute PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION. And since you expect the exception to be thrown by the prepare() method, you should disable the PDO::ATTR_EMULATE_PREPARES feature. Otherwise the MySQL server doesn’t “see” the statement until it’s executed. <?php $pdo = new PDO(‘mysql:host=localhost;dbname=test;charset=utf8’, ‘localonly’, ‘localonly’); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $pdo->prepare(‘INSERT INTO DoesNotExist … Read more

Cookies vs. sessions in PHP

The concept is storing persistent data across page loads for a web visitor. Cookies store it directly on the client. Sessions use a cookie as a key of sorts, to associate with the data that is stored on the server side. It is preferred to use sessions because the actual values are hidden from the … Read more

Edit PDF in PHP? [closed]

If you are taking a ‘fill in the blank’ approach, you can precisely position text anywhere you want on the page. So it’s relatively easy (if not a bit tedious) to add the missing text to the document. For example with Zend Framework: <?php require_once ‘Zend/Pdf.php’; $pdf = Zend_Pdf::load(‘blank.pdf’); $page = $pdf->pages[0]; $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); … Read more

PHP short-ternary (“Elvis”) operator vs null coalescing operator

When your first argument is null, they’re basically the same except that the null coalescing won’t output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say: The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary … Read more

Custom key-sort a flat associative based on another array

Just use array_merge or array_replace. array_merge works by starting with the array you give it (in the proper order) and overwriting/adding the keys with data from your actual array: $customer[‘address’] = ‘123 fake st’; $customer[‘name’] = ‘Tim’; $customer[‘dob’] = ’12/08/1986′; $customer[‘dontSortMe’] = ‘this value doesnt need to be sorted’; $properOrderedArray = array_merge(array_flip(array(‘name’, ‘dob’, ‘address’)), $customer); … Read more