background preloader

PHP: Connection handling - Manual

PHP: Connection handling - Manual

PHP How to do a POST request This example shows how to do a simple POST request to another webserver by using a socket connection. PHP Tutorial: Writing Your First PHP Script: A Feedback Form (a FormMail Script) Getting Started with PHP: Write a FormMail Script in PHP by Christopher Heng, thesitewizard.com I have always believed that the most fun way to learn a new programming language, whether it is a language like C or a scripting language like PHP, is to use it to write a real-life useful program. Of course this is not the most systematic method of learning, but it works well if you already have some background in programming. Preliminaries Before you write your first PHP program, make sure that your website is running on a web host that runs PHP 4.1 or above. I will begin with a very rudimentary (but working) PHP script to take input from a feedback form and send it to you in an email message. If you are programming-savvy, you will recognize this as a sort of "Hello World" program, but infinitely more useful! Writing the Feedback Form The first thing we need to do is to write the feedback form itself. The Feedback Form PHP Script Now all that remains is to code "sendmail.php". Conclusion

Creating excel files through PHP No need to use complicated or extensive libraries (such as PEAR) to create Excel pages. Just take advantage of the "smart" part of Excel; its ability to parse an HTML table to a nice Excel page. Just create a regular .PHP file, where you output your data in a nice little html-table. Then place the following snippet in the top of that file (no output can happen before these lines, as they change your headers -- so place these all the way at the top): header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=excel.xls"); And it's just that easy. If that's not enough, you can look at more extensive libraries such as PHPExcel.

Variable scope The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example: Here the variable will be available within the included script. <? function test(){ echo $a; /* reference to local scope variable */ } test();? This script will not produce any output because the echo statement refers to a local version of the variable, and it has not been assigned a value within this scope. The global keyword ΒΆ First, an example use of global: Example #1 Using global <? function Sum(){ global $a, $b; $b = $a + $b;} Sum();echo $b;? The above script will output 3. A second way to access variables from the global scope is to use the special PHP-defined array. Example #2 Using instead of global function Sum(){ $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];} Example #3 Example demonstrating superglobals and scope Note: Using global keyword outside a function is not an error. <? <? <?

PHP File Upload Configuration Though PHP presents a very versatile and user friendly interface for handling file uploads, the default installation is not geared for working with files in excess of 2 Mega Bytes. This article will help you configure your PHP engine for handling such large file transfers. The php.ini File All the configuration settings for your installation are contained in the php.ini file. You can call the phpinfo() function to find the location of your php.ini file, it will also tell you the current values for the following settings that we need to modify file_uploads upload_max_filesize max_input_time memory_limit max_execution_time post_max_size The first one is fairly obvious if you set this off, uploading is disabled for your installation. upload_max_filesize and post_max_size Files are usually POSTed to the webserver in a format known as 'multipart/form-data'. According to the PHP documentation you can set a MAX_UPLOAD_LIMIT in your HTML form to suggest a limit to the browser. memory_limit

Variable variables Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as: A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e. At this point two variables have been defined and stored in the PHP symbol tree: with contents "hello" and with contents "world". produces the exact same output as: i.e. they both produce: hello world. In order to use variable variables with arrays, you have to resolve an ambiguity problem. Class properties may also be accessed using variable property names. Curly braces may also be used, to clearly delimit the property name. Example #1 Variable property example <? $foo = new foo();$bar = 'bar';$baz = array('foo', 'bar', 'baz', 'quux');echo $foo->$bar . $start = 'b';$end = 'ar';echo $foo->{$start . I am bar. Warning

Writing a Template System in PHP (Page 1 of 9 ) Learn how to use templates in your web applications and implement them with object oriented code. Templating is an easy way to create large sites without much hassle in maintaining a consistent look and feel across them. While I was working on a recent project I came across the need for a basic templating script. The project needed something small and simple but also extensible. More Display Tutorials ArticlesMore By bluephoenix Debugging techniques for PHP programmers Introduction There are many PHP debugging techniques that can save you countless hours when coding. An effective but basic debugging technique is to simply turn on error reporting. Another slightly more advanced technique involves using print statements, which can help pinpoint more elusive bugs by displaying what is actually going onto the screen. PHPeclipse is an Eclipse plug-in that can highlight common syntax errors and can be used in conjunction with a debugger to set breakpoints. Setting up To learn the concepts described in this article, you are going to need PHP, a Web server, and Eclipse. We need a Web server to parse the pages you create in PHP and display them to the browser. To take advantage of some of the debugging techniques in this article, you need to install Eclipse V3.1.1 and the plug-in: PHPeclipse V1.1.8. You also need the debugger module extension for PHP. See Resources for download information. Back to top Error messages Error reporting in PHP Testing error reporting <?

Related: