Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to PHP & Variables

Similar presentations


Presentation on theme: "Intro to PHP & Variables"— Presentation transcript:

1 Intro to PHP & Variables
BIS1523 – Lecture 4

2 Web Programming The HTML pages we have seen so far are ‘static’; that is, they will have the same content every time you visit them. Any changes would have to be done to the HTML to change the content. Using a combination of HTML, and a programming language like PHP, we can create dynamic web pages that change, based on users input. For example, we could write a program (web page) that would allow the user to enter in a temperature in Fahrenheit, and our program would calculate and print the results of a conversion to Celsius.

3 Program Overview This program follows three basic steps, which are common to many programs written: Read in some data from the user (Input) Perform some sort of calculation on that data (Process) Display some result based on that calculation (Output) In this course, the tools we will use to accomplish this are: HTML: Which allows us to not only display results viewable on the web, but also has constructs that allow us to read in data from the user CSS: To provide detailed formatting of our input and output PHP: The programming language which will perform our processing

4 PHP: Server Side Processing
Let’s recall that our HTML (and CSS) web pages are stored on a server, and sent to the user for display. When we add a level of processing to this set up, we need to understand where (and when) the processing is taking place, and how that limits our input and output With PHP, the server runs the program before it is delivered to the user. So a normal sequence for a web page would be: Client sends a request to server Server finds the web page, and executes all the code in our PHP program Server sends final results, in the form of HTML to the client Client web browser displays results (using HTML and CSS)

5 PHP: File Naming Web pages with programs must be named differently than standard HTML Instead of having the .html extension, they should have the .php extension. Example: index.php Tempconv.php The extension tells the server that it needs to do some processing (run the php program) before sending it out to the client PHP program files will contain HTML just as a normal web page, but they will also contain PHP commands that dynamically generate content

6 Including HTML To include program code in your HTML, enclose it between the <? And ?> tags. These tags can be used more than once for multiple sections of code, and can appear in any section of your HTML.

7 Example: Hello World Here is an example with your first PHP command, the print command. The print command says “Send this stuff as output along with the HTML to the client”. Anything enclosed in double quotes is output. The result is a simple web page that says “Hello World!”

8 PHP Syntax Programming languages have a very specific way commands must be entered for the computer to run them correctly. This is called the syntax of the language. In PHP, for example, every command ends with a semicolon The commands in a PHP program are executed one at a time, in order from top to bottom. PHP doesn’t require it, but we will only be putting one command per line. This makes it easier for us to read.

9 Example Program The output from the PHP code looks like this:
Notice that it all appeared on the same line. This is because the output is interpreted like HTML, and in HTML, to split things up into multiple lines, or parts, you need to use tags (like <br> or <p>) The resulting HTML that was sent from the server to the browser is shown on the right

10 Example: Including HTML
Since, after the server processes our PHP program, the results are sent as HTML to the client, we typically are going to output HTML with proper tags. In the example above, we use the <h1> tag to specify that the first line is a header. This changes its appearance, as well as separating it from the next content section.

11 Mixing PHP and HTML You can include normal HTML in your php file as well as php-generated html. To the client, the resulting web page will look just like a normal HTML page. You should use normal HTML for everything in your document that you can, and try and only use PHP to generate content that might change.

12 Printing Quotes For some HTML tags, you will want to print double quotes within your output. This is problematic, as using double quotes in the print command and the output doesn’t work: print “<section id=“name”>” The best way of printing a double quote is to mark it so the server knows it is output rather than the end of the print marker. To do this, you ‘escape’ it using the backslash character. print “<section id=\”name\”>” This will result in the correct HTML output of: <section id=“name”>

13 Checking Your Output When trouble shooting your PHP programs, it is often useful to be able to view the HTML that was printed out. You can do this in most browsers by right clicking the page and selecting “view source” In the course UI, it is best to open your page in a new window, then do this:

14 Viewing HTML In the new tab, right click the page and select view source, and you will get a display like below, showing the HTML that your program sent. This way you can see if there are missing tags, double quotes, or other things that might be in error.

15 Printing newlines in HTML
In the previous example, this code: Produced this HTML: Notice both tags are on the same line. It would be easier to read if we separated them into multiple lines. Note: The browser will display the HTML the same, it is just easier for us to test and debug. To print a “line break”, add the \n character to your printouts

16 Printing newlines in HTML
Now with this code: We get this HTML:

17 Variables A variable is a container for data. Once data has been stored in a variable (or, stated more accurately, once a variable has been assigned a value), that data can be altered, printed to the Web browser, saved to a database, ed, and so forth. Variables in PHP are, by their nature, flexible: You can put data into a variable, retrieve that data from it (without affecting the value of the variable), put new data in, and continue this cycle as long as necessary. But variables in PHP are largely temporary: Most only exist—that is, they only have a value—for the duration of the script’s execution on the server. Once the execution passes the final closing PHP tag, those variables cease to exist.

18 Variable Syntax All variable names are preceded by a dollar sign ($)
Following the dollar sign, the variable name must begin with a letter, or an underscore (_). It can’t begin with a number. The rest of the variable name can contain any combination fo letters, underscores, and numbers. You may not use spaces within the name of a variable. Each variable must have a unique name Names are case-sensitive. So $variable and $Variable are 2 different variables. It’s usually a bad idea to name two variables so similarly, so I recommend using all lower case letters in variable names.

19 Variable Names

20 Types of Variables There are three main types of variables in PHP, numbers, strings, and arrays. Numbers hold numeric values. They are typically expressed without double quotes around them. Fractions are only expressed as decimal places. Strings hold any number of characters contained between single, or double quotes. A string can contain single or double quotes by escaping them (the same way we do in the print command.) Arrays (covered in more detail later) can store more than one value. You might think of them as a table, where each row is numbered, or each row is named with a key value.

21 String and Number Examples
Valid Numbers 1 1.0 1972 -35.25 Valid Strings “Hello World!” ‘Hello World!’ “<section id=\”name\”>” “” Notice that numbers don’t use double quotes. The last string example is known as the “empty string”, a string that contains no characters.

22 Array Examples Arrays in PHP can be indexed, or associative
In an indexed array, each row of the table is numbered. In an associative array, each row is named with a “key” value. Each of these example arrays is storing four separate values, which may all be accessed individually.

23 Storing a Value in a Variable
Since arrays are containers for data, we need ways to both store a value in the container, as well as retrieving a value from it. To store a value in a variable, we use the assignment operator: the equals (=) sign. The name of the variable goes on the left, and the value that you want to put into the variable goes on the right. For example: Each assignment ends in a semicolon, and we put each one on a different line. These three assignments store 3 values into 3 variables. The first two are string variables, the last is a number.

24 Retrieving a Value from a Variable
To print out a value of a variable, you can use the print command with the variable name: If you want to print a variable’s value within a context, you can place the variable’s name in the printed string, as long as you use double quotation marks In this case the value of the variable would be substituted where its name appears, so the output would be:

25 Example The first three lines store values into the variables.
The print commands access those values to be printed HTML tags are included to put name, address, and courses on different lines (using the <br> tag)

26 Single vs Double Quotes
In PHP, single and double quotes are treated differently. Items within double quotes are parsed. So within any string with double quotes, variable names are replaced with their values. Items within single quotes are treated literally, so variable names within single quotes would be printed out as names, not values. This rule applies everywhere within PHP. Most of the time in this course we will use double quotes.

27 Assignments with Quotes
Code Output Notice that parsing occurs everywhere in PHP, not just as part of the print command. So in the assignment above, the string on the right is parsed before the value of greeting is set. So greeting ends up with the value: “Hello Steve Canfield, Welcome to our program” Also note, the command print $greeting is functionally equivalent to print “$greeting”, the double quotes aren’t really necessary


Download ppt "Intro to PHP & Variables"

Similar presentations


Ads by Google