MySQL lecture #3 Aaron Gabow If you have any questions, you can send them to gabow@cbio.mskcc.org or you can call me at (646)888-2622 Background PHP is programming language that allows for rapid development of web pages and for applications that access SQL databases. It is free to download and use, and it‟s available for Windows, Linux, Mac OS X and more. If you have any programming background, PHP will be pretty intuitive to you. That‟s all the good news. The bad news is that PHP isn‟t that great for more than writing middle-sized web applications with database backends. The worse news is that I‟m not a PHP expert, so there is a chance I‟ll be stumped by your questions. If this does happen (and honestly, it will happen), I‟ll research the answer and get back to you. A Basic PHP Program
There are a few things worth noticing about this simple program. PHP code lives within an HTML document. and are both HTML tags. The only parts of this PHP file that are actually PHP are within the tags. The echo function takes as input a string and then returns that string into the HTML document. Consequently, this line of code is equivalent to writing the HTML
Hello, world!
.
. . In fact, this HTML is exactly what a web browser accessing this file sees. But bon‟t take my word for it – access this file and check out “View -> Page Source”. Another thing worth noticing is the types of comments you can use in PHP. Lines with //, /*…*/ or # are all considered comments. PHP doesn‟t evaluate them; they aren‟t even transmitted to the web browser accessing the file. Remember when commenting your code (and you should be commenting your code), PHP knows the above comment symbols; HTML does not. This distinction means that when you want to use those comment symbols, they must be inside a PHP tag. To add comments to HTML outside of PHP tags, use . Lastly, notice the file name ends with a .php. A web server needs to know it‟s opening a PHP file and not a badly formatted HTML file, or some other sort of scripting language. The standard way of indicating this is with a .php, .php4 or .php5.
PHP knows math
This program basically does the same thing intro.php did. It does include some new functions and introduces variables. In PHP, variables start with “$” and are followed by alphanumerics and underscores. There are four basic types in PHP and a variable can take on any of those types, even if it has been a different type earlier in the program. Variables defined within one set of tags remain in scope for usage in other PHP tags. For example, $area is defined in the first PHP tag and then displayed in the second block of PHP code. We use a few of php‟s built-in math functions in the program, but there are more, including: abs, cos, decbin, dechex, decoct, deg2rad, floor, log10, log, max, min, rand, sin. The printf function works like echo but it gives you more control over how you want the data displayed. If you wanted two decimal places, you would use %.2f. For a complete list of printf‟s formatting options, consult http://us2.php.net/manual/en/function.sprintf.php Those with real sharp eyes might have spotted the <=? … ?> tag. This tag is placed within the HTML to quickly display the value of a PHP variable in the page, in this case $area_improved. Do you know bugs?
This code has several bad bugs in it. One will prevent the code from running, the will cause an undesired result.
You probably want to change the error reporting level when writing code. You probably don‟t want to for production-level code. Arrays are a composite of types
Arrays are a compound type. In PHP an array is a mapping between an integer and another PHP type. The line echo “I‟m proud to be a $organisms[3]
” demonstrates this idea. The previous line mapped the key 3 to the value “Homo sapien” and that value is now retrieved using the array value indexed at that key. PHP arrays also allow strings as keys, as shown in the line $common_names = array('Homo sapien' => 'human', 'Rattus norvegicus' => 'rat'); The underlying treatment of array keys is identical for both types, so you can mix and match types in a single array, although I personally would not. Since PHP allows an array value to be any type, and arrays are types, PHP allows for arrays of arrays. This code also introduces a new syntactic element, “{…}”. Within a string, the curly braces indicate that the text within the braces should all be treated as PHP code. Using the $ only the text up to an invalid variable name element, such as “ " or “‟” is treated as
PHP code. This is because the $ can only indicate a single variable within a string. Curly braces can indicate a single variable, but they can also indicate complex expressions. In this case, the complex expression is the array and an array key. PHP has one other composite type, objects. Personally, while I like objects (which you might have heard of in the phrase “object-oriented programming”), they feel very tacked on in PHP and I don‟t use them. If you want to learn more about objects, they are discussed at: http://us2.php.net/manual/en/language.types.object.php Dynamic content
An HTML form
And its corresponding PHP code. Notice that the HTML form calls the php program on the line: