MySQLDay3

Reviews
Shared by: Guillaume
Tags
Stats
views:
55
rating:
not rated
reviews:
0
posted:
11/15/2007
language:
pages:
0
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: The PHP code itself does a few things we haven‟t talked about yet. The variable $_REQUEST is a superglobal variable. A PHP program can always be access the variable to find out the values mapped to string keys by a form submission. We also use the if, elseif control structure. The idea here is if the statement within the first clause is true (if $institute has the value “msk”), then execute whatever code is in-between the {…}. If the if statement was false, the program will look to see if the condition in the first elseif is true. If it is, it will execute the code within those curly braces. If it isn‟t, the program will look to see if the condition in the last elseif is true. Since the form can only assign $institute one of these variables, I don‟t include an else clause. If I wanted to be really safe, I could add one in after the last elseif statement. The other line that is worth mulling over is: which has two new things in it. The tag is simply shorthand for . It‟s more convenient to write, but depending on server configurations, some servers won‟t recognize the tags, while every PHP-enabled server knows . The other new thing is the include statement. It allows you to pull out PHP code into reusable chunks that get executed wherever they are inserted into a file. One nice thing about includes, is a variable in the including file are automatically available in the included file. For example, I could use $institute in legal.php, and the program would know what I‟m talking about. PHP Functions We‟ve already seen examples of PHP functions throughout the examples, such as echo and pow. Functions work similarly to the functions we‟ve seen in perl and to functions in mathematics – they map an input to exactly one output. While php comes with various useful functions already in the language you might find yourself wishing to create new functions. The lay the language handles (what programmers call syntax) declaring a function is pretty simple. All you need to do is declare the input the function takes and the returned mapping. In the code above, I declared the function annoyify that executes a group of string functions on the string passed in as a parameter. I first user strtoupper to make the string all upper-case letters, then I concatenate the string with some html tags, and the I use preg_replace to do a perl-style regular-expression-based replacement of „.‟ with the more extreme „!!!!1‟. In a program, I would create the annoyify function if I was going to use this same string transformations on several different strings. Also, in an actual program, I would have combined the string transformations into two lines of code: $transformed_string = “” . strtoupper($transformed_string) . “;” $transformed = preg_replace(„/\./‟, „!!!!1‟, @transformed_string); but breaking it into four lines makes it easier to display. For a complete listing of standard PHP string functions check out http://us2.php.net/strings. Installing PHP To get PHP up and running, you‟ll need:  a web server  PHP software. You should get the latest stable release 5.2.3 (Windows) or 5.2.2 (Mac). Macs come with a version of PHP already, but it might be old. My Mac has 4.4. To test your version, in a Mac terminal type “php -version". If you get an error message you might not have php installed, or it just might not be in your execution path. You can get the latest php for windows at: http://www.php.net/downloads.php And for Mac at: http://www.entropy.ch/software/macosx/php/ If you have a Mac that comes pre-installed with Apache and PHP, you might need to alter your Apache configuration files to enable PHP. In my case, I had to sudo -s and then edit my /etc/httpd/httpd.conf file, removing the “#” on lines: #LoadModule php4_module and #AddModule mod_php4.c A more detailed, but slightly out-of-date, discussion of altering a Mac Apache configuration file is found at: http://www.devarticles.com/c/a/Apache/Using-Apache-and-PHP-on-Mac-OS-X/ If all of this sounds like crazy talk, look at pages 1-4 of your PHP Pocket Reference or ask your System Administrator. Acknowledgements: These handouts were based on the PowerPoint slides of Ethan Cerami, which in turn were based on the information from http://dev.mysql.com/tech-resources/articles/ddws


Shared by: Guillaume
Other docs by Guillaume
YouTube-039-s-Official-Authorities-The-Users-70079
Views: 1672  |  Downloads: 12
YouTube-Fights-Against-Its-Father-Google-55082
Views: 1400  |  Downloads: 11
xna_launch_final_report
Views: 1361  |  Downloads: 5
XNA_Introduction
Views: 1101  |  Downloads: 11
xna
Views: 1032  |  Downloads: 4
XNA Development-1
Views: 1851  |  Downloads: 10
xmas_05
Views: 977  |  Downloads: 0
xerc_users_manual
Views: 1086  |  Downloads: 1
xbst
Views: 1027  |  Downloads: 0
Xbox Way
Views: 1095  |  Downloads: 0
XboxVGA Video Setup
Views: 557  |  Downloads: 0
xbox-router
Views: 373  |  Downloads: 0
xboxnext_security
Views: 248  |  Downloads: 2
XBoxMACAddress
Views: 917  |  Downloads: 0