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. 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 PHP5 version Source : 01. 04. 05. 08. 11.
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.
Simple Layouts with PHP
This tutorial is for those starting out in PHP and want to learn a great way to create layouts. This will help reduce redundancy and keep you from writing too much code. Download the Source Anyone that has been developing HTML based sites for some time has undoubtedly ran in to the issue of keeping the design consistent across multiple pages. Technique #1 A lot of sites follow this kind of layout scheme The first technique is the entry level solution that works across nearly all situations. <! Take out everything up to the point and put that in a new file called head.php. <? Now just put whatever HTML/PHP content you want in between those two include statements and check it out in a browser. It may be easier to think of it as a puzzle, you are piecing together a complete HTML page with includes. Technique #2 Alright we have that under our belt let’s look at another solution. <! This will be your index.php, but the content will change based on the links the user goes to. Conclusion
PHP Help Tutorial: PHP Basic Pagination
Basic Pagination As a web developer, you will often be tasked to display large amounts of data to the user in some kind of easy to read format. Let's say for instance you have a list of employees in your database, and you want to be able to list them on your web page. If you only have a dozen or so employees, it's no big deal to just make a simple loop and display them all on the same page, right? Well what happens when you have 50 employees? Pulling out all that data at the same time can leave your user tapping his fingers on the desk wondering what the frak is taking so long, and when he finally does get his info, it's a whole frakking novel on one page! Well anyways, it makes way more sense to break up your list into page-sized chunks, and only query your database one chunk at a time. First things first, you need a table with some data in your database to work with. If you opted to make a new table with that info, here's a quick and dirty script to get it populated: <? Why 3 digits? <?
Absolute paths for PHP include
PHP Examples - snippetdb.com
passing variables via url Pass variables to a page using the question mark, for example mypage.php?value=hello. Access these variables with the _GET command. send email Send an email from PHP - one line example. redirect browser Using the php header function, a browser can be redirected to another page. get current filename as variable Sometimes you need just the filename of the current script, without the path. email validation, simple Validate an email address - returns TRUE or FALSE. get current page URL Often used for form actions, the variable $PHP_SELF has changed with recent versions, and now must be accessed using the _SERVER array of pre-defined variables. form action - detect form post There are several ways to process a php form. generate random number Generate a random number using mt_rand. force cookies, disable php sessid in URL This commands disables php from rewriting URLs to add a phpsessid, and forces the use of cookie sessions instead. show referring URL Define A Constant
PHP Tutorials: Variables - Some Practice
In the previous section, you saw what variables are: storage areas to hold things like numbers and text. You tell PHP to remember these values because you want to do something with them. In this section, you'll get some practice using variables. Off we go. Testing variables with PHP First, we'll take a look at how to display what's in your variables. <html> <head> <title>Variables - Some Practice</title> </head> <body> When you've finished typing it all, save the page as variables.php. If you've created a folder inside the www folder, then the address to type in your browser would be something like: If you were successful, you should have seen the text "It worked!" The PHP script is only one line long: The rest of the script is just plain HTML code. We've put the PHP in the BODY section of an HTML page. So you need a left angle bracket ( < ) then a question mark ( ? To display things on the page, we've used print( ). <?