Download presentation
Presentation is loading. Please wait.
Published byFrank Bradley Modified over 9 years ago
1
COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 03
2
COMP6015 - An Introduction to Computer Programming : University of the West Indies Programming Concepts
3
COMP6015 - An Introduction to Computer Programming : University of the West Indies Programming Concepts Variables: Memory locations which can hold data temporarily. The value being held can be changed overtime. Variables must be declared before they can be used.
4
COMP6015 - An Introduction to Computer Programming : University of the West Indies Programming Concepts The Order of precedence: 1.Left to right 2.Parentheses 3.Multiplication / division 4.Addition / subtraction
5
COMP6015 - An Introduction to Computer Programming : University of the West Indies Programming Concepts Flow of operation: 1.Sequential - Generally Start to End 2.Selection: if condition { … } else { … }
6
COMP6015 - An Introduction to Computer Programming : University of the West Indies Programming Concepts Flow of operation: if (condition) { … } else if (condition) { … } else { … }
7
COMP6015 - An Introduction to Computer Programming : University of the West Indies Programming Concepts 3.Iteration/Looping: 1. for ( ; ; ) { …. } e.g. for (x = 0; x < 10; x++) { print x; }
8
COMP6015 - An Introduction to Computer Programming : University of the West Indies Programming Concepts 3.Iteration/Looping: 2. while (condition) do { …. } e.g. while (x < 0) do { print x; x = x+1; }
9
COMP6015 - An Introduction to Computer Programming : University of the West Indies Programming Concepts 3.Iteration/Looping: 3. do { …. } while (condition) e.g. do { print x; x = x+1; } while (x < 0)
10
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP
11
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP What is PHP? PHP stands for PHP: Hypertext Preprocessor A server-side scripting language for creating dynamic Web pages. You create pages with PHP and HTML. When a visitor opens the page, the server processes the PHP commands and then sends the results to the visitor's browser, just as with ASP or ColdFusion. However, unlike ASP or ColdFusion, PHP is Open Source and cross-platform.
12
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP PHP runs on Windows NT/2000/XP and many Unix versions, and can be built as an Apache module and as a binary that can run as a CGI. When built as an Apache module, PHP is especially lightweight and speedy. Without any process creation overhead, it can return results quickly, but it doesn't require the tuning of mod_perl to keep your server's memory image small. In addition to manipulating the content of your pages, PHP can also send HTTP headers. You can set cookies, manage authentication, and redirect users.
13
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP PHP offers: excellent connectivity to many databases (and ODBC), integration with various external libraries that let you do everything from generating PDF documents to parsing XML. PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal: Allow web developers to write dynamically generated pages quickly.
14
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP PHP goes right into your Web pages, so there's no need for a special development environment or IDE. You start a block of PHP code with <?php and end it with ?> You can also configure PHP to use ASP-style tags or even The PHP engine processes everything between those tags.
15
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP PHP's language syntax is similar to C's and Perl's. You don't have to declare variables before you use them, and it's easy to create arrays and hashes (associative arrays). PHP even has some rudimentary object-oriented features, providing a helpful way to organize and encapsulate your code.
16
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP NOTE: Although PHP runs fastest embedded in Apache, there are also instructions on the PHP Web site for seamless setup with: Microsoft Internet Information Server (IIS) and Netscape Enterprise Server. Our concentration will be with APACHE.
17
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Creating Your Own Commands PHP scripts sit inside HTML documents, therefore you don't need a special editor to create pages. However, you do need to be running on a server that supports PHP. If you run your own server, this is easy enough to do. If an ISP will be serving your pages, you will need the ISP's support team to install PHP for you.
18
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP What Is Required? For Unix/Linux systems, you'll need basic Unix skills, such as using make and a C compiler, an ANSI C compiler on your system, and a Web server. For Windows, you'll need one of the following servers: Microsoft Personal Web Server, Microsoft Internet Information Server, Apache or Omni HTTPd.
19
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP PHP's basic syntax is familiar. <?php echo "Hello, World!"; ?> produces Hello, World!
20
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Variables are marked with a preceding $. You could write "Hello, World!" like this: <?php $message = "Hello, World!"; echo $message; ?>
21
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP String concatenation is done with. (a period); other arithmetic operators are what you would expect: <?php $greeting = "Hello "; $num = 3 + 2; $num++; echo "$greeting $num people!"; ?> Produces Hello 6 people!
22
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP String concatenation: <?php $a = "Hello "; $b = $a. "World!"; // now $b contains "Hello World!" $a = "Hello "; $a.= "World!"; // now $a contains "Hello World!" ?>
23
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Just as in Perl, a string surrounded with double quotes causes variables inside it to be interpolated, but a string surrounded with single quotes does not. <?php $name = 'Susannah'; $greeting_1 = "Hello, $name!"; $greeting_2 = 'Hello, $name!'; echo "$greeting_1\n"; echo "$greeting_2\n"; ?> Produces Hello, Susannah! Hello, $name! Note that the \n in the string turns into a new line, just as in Perl or in C. This only works in double- quoted strings, however.
24
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Environment Variables PHP makes environment variables available to you as regular variables. This includes the environment variables that are set by the server for a Common Gateway Interface (CGI) program (even if PHP is being run as a module). e.g., if the page http://www.domain.com/farm/cattle/cow-cow.cow.html contains the code <?php echo "[$REQUEST_URI]"; ?> it prints out [/farm/cattle/cow-cow-cow.html]
25
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Arrays You set off array indices (regular or associative) with square brackets ([ and ]): e.g. $fruit[0] = 'banana'; $fruit[1] = 'papaya'; $favorites['animal'] = 'turtle'; $favorites['monster'] = 'cookie';
26
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Arrays If you assign something to an array but leave the index blank, PHP assigns the object onto the end of the array. The statements about $fruit, in the previous slide, produce the same result as: $fruit[] = 'banana'; $fruit[] = 'papaya';
27
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Arrays You can have multidimensional arrays, too: $people['David']['shirt'] = 'blue'; $people['David']['car'] = 'minivan'; $people['Adam']['shirt'] = 'white'; $people['Adam']['car'] = 'sedan';
28
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Arrays A shortcut for creating arrays is the array() function: $fruit = array('banana','papaya'); $favorites = array('animal' => 'turtle', 'monster' => 'cookie); NOTE: 'animal' => 'turtle’ means that subscript 'animal' is assigned the value 'turtle'
29
COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP OR $people = array ('David' => array('shirt' => 'blue', 'car' => 'minivan'), 'Adam' => array('shirt' => 'white', 'car' => 'sedan')); The built-in function count() tells you how many elements are in an array: $fruit = array('banana','papaya'); print count($fruit); Prints2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.