1 / 25

LIS651 lecture 1 introduction to PHP and string variables

Learn the essentials of PHP programming, including form data handling and understanding PHP as a server-side scripting language. Dive into PHP basics such as variables, functions, and creating dynamic web pages.

Download Presentation

LIS651 lecture 1 introduction to PHP and string variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thomas Krichel 2010-01-28 LIS651 lecture 1introduction to PHP and string variables

  2. today • Introduction to the course • Introduction to PHP • Using form data in PHP

  3. today • We introduce PHP. Understanding PHP is the most difficult aspect of the course. • We look at how PHP can be used to show the data that we get from the form. • You should think about what data to get and how to show it. • Everybody will build an example form and then a PHP script to show it. • Finally we build a new PHP script that contains the form. So instead of two files, we only have one.

  4. PHP introduction • PHP is the PHP Hypertext Processor. • It is a tool that allows for server-side scripting. • Its predecessor is PHP/FI, Personal Home Page / Forms Interpreter. • PHP/FI was released by Rasmus Lerdorf in 1995. It was written in Perl. • PHP/FI version 2 was released in 1997. It was written in C. • PHP version 5 is the current version.

  5. Apache and PHP • When a file ends in .php, is not simply sent down the http connection like other files. • Instead, apache sends the file to the PHP processor. • It sends to the client whatever the PHP processor returns. • The PHP processor is a module that lives inside Apache.

  6. PHP language • PHP is an interpreted language. • You write a series of statements. • Apache hands these statements to the PHP interpreter. • The interpreter executes these statements one by one. • When it find an error, it stops running and signals the error. • Compiled languages are different. They read the whole program before starting to execute it.

  7. try it out • Remember we duplicate validated.html when creating a new new file. • Right-click on validated.html, choose duplicate. • You may be asked to supply your password again. • You erase the contents of the dialog box that suggests a new file name and put your new file name in there. • If it contains PHP code, it has to end in .php.

  8. first PHP script • Create a file with the name info.php, and the following contents <?php phpinfo(); ?> • nothing else. This will create a test page that tells you everything PHP knows about. Look at some of the variables.

  9. comment on info.php • In terms of XML, the "<?php" until "?>" part is called a processing instruction. It is a type of node that we did not encounter in LIS650. • We can call any part of the file between "<?php" and "?>" a PHP part of the file. • The XML file here contains just the processing instruction.

  10. output of phpinfo()‏ • phpinfo() create a whole web page for you, that validates against a loose HTML specification. • That page contains a lot of technical detail. • The section we may be interested in is “PHP Variables”. It contains variables that we may be interested in. These are variables that PHP can understand • from its environment • from the client

  11. the magic of PHP • The client never sees the PHP code. It only sees what the PHP processor has done with the code. • You can write normal HTML code, and you can switch to writing PHP code pretty much at any stage. • You can have several PHP parts. • PHP parts can not be nested. • The contents of the PHP part can be called a PHP script.

  12. statements • Like a normal text is split into sentences, a PHP script is split into statements. • A PHP script contains one or more statements. • Each statements tells the interpreter something. • Each statement is ended by a semicolon. • In our first script there is only one statement. • Each statement is ended with a semicolon! • Think of a statement like a rule in CSS. But never forget the semicolon!

  13. expressions • The stuff before the semicolon is called an expression. • You can think of an expression as anything anyone may want to write in a computer program. • So an expression is just a way to talk about “stuff” in a program in a more edifying way than just calling it “stuff”.

  14. functions • phpinfo() is a function. • Functions are one of the most fundamental concepts in computer programming. • A function is an expression that does something to something else. The “something else” is in the parenthesis. It is called the argument of the function. • The argument of phpinfo() is empty.

  15. second php script: hello.php • Normally we write HTML code and then we add PHP parts. • Take validated.html, copy to hello.php • make the body <div> <?php print("Hello, world!"); ?> </div> • Validate the resulting XHTML.

  16. comment on hello.php • print() is also a function. print() prints its argument. Here the argument is a string. A string is a sequence of characters enclosed by single or double quotes. • For print, the () can be omitted. • You could have written three statements <?php print "<div>"; print "Hello, world!"; print "</div>"; ?>

  17. good style • Write each statement on a new line. • Add plenty of comments. There are three styles of comments in a PHP program • // the rest of the line is a comment • # the rest of a line is a comment • /* this is a comment */ • Only last style can be used over several lines. • Do you recognize two of the commenting styles?

  18. another way to write hello.php <?php $greeting="Hello, world!"; print "<div>$greeting</div>"; ?> • Here $greeting is a variable. The first statement assigns it the string value "Hello, world!". The second statement prints it out. • This example is important because it illustrates the concept of a variable. • The name of the variable is greeting.

  19. variable names • Variable name must start with a letter or underscore. They can contain letters, digits and underscores. The following are examples of illegal names • $2drunk • $bottle-content • $brewer@grosswald • Variable names are case sensitive. I use lowercase only and add underscores in long names. • The variable name "$this" is reserved. • It is good to give variables meaningful names.

  20. strings • a piece of text in PHP is called a string. • A string is often surrounded by single quotes. print 'I want beer'; $want='beer'; print 'I want $want'; // prints: I want $want • If you want to use the values of variables, use double quotes $want='beer'; print "I want $want"; // prints: I want beer

  21. single and double quotes • You can use single quotes to quote double quotes print 'She wrote: "I want beer." and sighed.'; // prints: She wrote: "I want beer." and sighed. • and vice versa print "She wrote: 'I want beer.' and sighed"; // prints: She wrote: 'I want beer.' and sighed. • Sometimes it is not obvious when to put single quotes, double quotes, and when to leave them out. If one thing does not work, try something else.

  22. the backslash escape • The backslash is used to quote characters that otherwise are special. print 'Don\'t give me bad beer!'; $kind='bock'; $beer='Festbock'; print "<p class=\"$kind\">$beer</p>"; // prints: <p class="bock">Festbock</p> • The backslash itself is quoted as \\ print "a \\ against beer consumption"; // prints: a \ against beer consumption

  23. more backslash escapes • \n makes the newline character • \r make the carriage return (no use in Unix)‏ • \t makes the tab (seldomly used in HTML)‏ • \$ makes the dollar (used in the shop)‏ • $amount='1.50'; • print "you owe \$$amount per bottle."; • // prints: you owe $1.50 per bottle. If the backslash was not there $ would be considered to be a variable.

  24. concatenation • This is done with the . operator. It puts two strings together, the second one after the first one. $cost='5.23'; $message='This costs ' . $cost; print $message; // prints: This costs 5.23

  25. Thank you for your attention! Please switch off computers when you are done! http://openlib.org/home/krichel

More Related