What are the backtick operators (“) called in the context of evaluating their content? Answer If you’re referring to Bash then the backticks are known as “command substitution”. $() provides similar functionality.
Tag: php
PHP XSS sanitization
Questions: What are the best safe1(), safe2(), safe3(), and safe4() functions to avoid XSS for UTF8 encoded pages? Is it also safe in all browsers (specifically IE6)? . Many people say the absolute best that can be done is: . . There are a hell of a lot of posts about PHP and XSS. Most just say “use HTM…
PHPExcel very slow – ways to improve?
I am generating reports in .xlsx using PHPExcel. It was okay in the initial testing stages with small data sets (tens of rows, 3 sheets), but now when using it on a real production data with over 500 rows in each sheet, it becomes incredibly slow. 48 seconds to generate a file, and when running a report that …
Best way to give a variable a default value (simulate Perl ||, ||= )
I love doing this sort of thing in Perl: $foo = $bar || $baz to assign $baz to $foo if $bar is empty or undefined. You also have $foo ||= $bletch which will only assign $bletch to $foo if $foo is not defined or empty. The ternary operator in this situation is tedious and tiresome. Surely there’s a simpl…
PHP regular expression
What is purpose of the following code? What kind of $string match this expression? Why there is a character @? Answer That regular expression will match any <script>…</style> or <style>…</style> (X)HTML blocks in the string and remove them. This is most likely done to preve…
Way to verify if variable is a valid GD image resource?
I have a class that accepts a GD image resource as one of its arguments. As far as I know, there is no way to type hint this since it is a resource and not an object. Is there a way to validate …
Mock private method with PHPUnit
I have a question about using PHPUnit to mock a private method inside a class. Let me introduce with an example: How can I stub the result of the private method to test the some more code part of the public function. Solved partially reading here Answer Usually you just don’t test or mock the private &a…
Are numeric and associative arrays in PHP two different things?
This is a deeper dive into a previous question I had here: Can items in PHP associative arrays not be accessed numerically (i.e. by index)? According to W3Schools, : In PHP, there are three kind of arrays: Numeric array – An array with a numeric index Associative array – An array where each ID key…
Altering .htaccess with PHP – removing a rewrite rule
I am using PHP to remove/add static pages once a page has been deleted, I want to be able to remove it from the .htaccess, however I’ve tried this, but it throws an error: Warning: preg_replace() [function.preg-replace]: Unknown modifier ” in … The code: This is an example of what it should …
Error in mysql fetch error in php
Hi when i run my code this proplem apper this is my code Answer rewrite so this: becomes this: