15 very useful PHP code snippets for PHP developers | ViralPatel.net. Following are list of 15 most useful PHP code snippets that a PHP developer will need at any point in his career. Few of the snippets are shared from my projects and few are taken from useful php websites from internet. You may also want to comment on any of the code or also you can share your code snippet through comment section if you think it may be useful for others. 1. Send Mail using mail function in PHP 2. 3.
The above code will not work in case your client is behind proxy server. 4. This function will return the duration of the given time period in days, hours, minutes and seconds. e.g. secsToStr(1234567) would return “14 days, 6 hours, 56 minutes, 7 seconds” 5. 6. Required Extension: SimpleXML 7. 8. Following is the PHP code to create the JSON data format of above example using array of PHP. Following code will parse the JSON data into PHP arrays. 9. 10. This basic snippet will create a random authentication code, or just a random string. 11.
Validate a date in “YYYY-MM-DD” format. PHP command line syntax checking. Posted April 24th, 2010 in PHP The PHP CLI (command line interface) allows basic syntax checking of a PHP file. I'll show the basic usage of this in this post and then a couple of ways to check all files in a directory, and all files recursively down the directory tree as posted by a couple of people on Twitter. Syntax checking a single PHP file from the command line The syntax for checking is like this, where the -l flag is a lower case L: If the syntax in the file is correct you will see this: If there are errors then theses will be listed as in the following example output: Note that the syntax checking only checks for syntax errors - it does not report the usage of variables which have not yet been defined, functions or class which have not be declared etc. So something like this will pass: but these will not: Syntax check all PHP files in the current directory.
PHP snippets. Extremely useful PHP classes. PHP PSD Reader A few weeks ago, I wrote an article about this PHP which allow you to display any Adobe PSD file on screen. Very usefull to create preview of PSDs designed for clients, for example.Download Browser detect One of the most common (and boring) problem for front-end developers is definitely cross-browser compatibility. This PHP class will detect almost all browsers and simplify your cross-browser work.Download Akismet Remember those days without spam? ADOdb The large majority of websites and web apps are using databases to store all kinds of data. HTML Purifier As it name tells, HTML Purifier is a PHP class created to help you writing a better code.
Google charts API Charts are very useful and highly asked by clients, but they can be a lot of work. pChart pChart is another chart class, and it is as good as Google charts API. PHP Excel Excel documents are highly popular in the corporate world. Country from IP Cache Manager WPGet. 10 code snippets for PHP developers « The html blog. I’ve compiled a small list of some useful code snippets which might help you when writing your PHP scripts… Email address check Checks for a valid email address using the php-email-address-validation class. Source and docs: 01.include('EmailAddressValidator.php'); 03. 04.if ($validator->check_email_address('test@example.org')) { 07.else { Random password generator PHP password generator is a complete, working random password generation function for PHP. 01.function generatePassword($length=9, $strength=0) { 02. 03. 04. if ($strength & 1) { 05. 07. if ($strength & 2) { 08. 10. if ($strength & 4) { 11. 13. if ($strength & 8) { 14. 17. 18. 19. for ($i = 0; $i < $length; $i++) { 20. if ($alt == 1) { 21. 22. 23. } else { 24. 25. 28. return $password; Get IP address Returns the real IP address of a visitor, even when connecting via a proxy. 01.function getRealIpAddr(){ 02. if (!
04. 06. elseif (! 08. 10. else{ 11. 13. return $ip; XSL transformation 01. 04. 05. 08. Sample PHP page. I'll start out by explaining some of the things I don't like about PHP. If I get this all out of the way, you can get to the useful stuff. I'll use this opportunity to say something instructive about how to use the language, based on its strengths and weaknesses, though, so it's not wasted time entirely. 1. PHP's syntax is weak. There will be times this will feel limiting. 2. PHP's primitive function set is huge. 3. 4. 5. 6. 7. 8. 9. For a first "assignment" to practice your incipient PHP skills after reading this tutorial, try writing a script that generates a webpage with this text file as the displayed content, formatted so that it is recognizable, readable, and clear on-screen.
Note that some of what I say here may seem incomplete to an experienced PHP programmer. ##### embedding in XHTML: PHP is easily embedded in XHTML (or HTML) via some special script tags that identify their contents as PHP code. ##### comments: ##### variables and values: echo '<a href="url.html">Visit Bob\'s URL! The Ternary Operator | Practical PHP Programming. There are three operators that are complicated enough to get their own section, of which the first is the ternary operator.
It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false. If that sounds like an if statement to you, you are right on the money - the ternary operator is a shorthand (albeit very hard to read) way of doing if statements. Here's an example: First there is a condition ($age < 16), then there is a question mark, and then a true result, a colon, and a false result. If $age is less than 16, $agestr will be set to 'child', otherwise it will be set to 'adult'. That one-liner ternary statement can be expressed in a normal if statement like this: So, in essence, using the ternary operator allows you to compact five lines of code into one, at the expense of some readability.
Next chapter: The scope resolution operator >> Previous chapter: Complete operator list Jump to: Home: Table of Contents. Complete operator list | Practical PHP Programming. As this book aims to be a complete guide to PHP, it would not be right not to list the entire selection of operators in the language, so here goes: 3.12.3.1 Arithmetic Operators If you have not used modulus since school, here's a quick refresher.
To calculate $a % $b, you first perform $a / $b and then return the remainder. For example, if $a were 10 and $b were 3, $b would go into $a 3 whole times (making nine) with a remainder of 1. Therefore, 10 % 3 is 1. 3.12.3.2 Assignment operators The difference between a standard assignment and references is explained in detail later - for now, you just need to know that a normal variable holds its own value, whereas a reference takes its value from another variable. 3.12.3.3 Bitwise operators Bitwise operators aren't used very often, and even then only by more advanced PHP programmers. The number eight, for example, is represented in eight-bit binary as 00001000.
Therefore, 52 & 28 gives 20. 3.12.3.4 Comparison operators Foo is 6 Bar is 5 Jump to: