Download presentation
Presentation is loading. Please wait.
Published byEdmund Nichols Modified over 9 years ago
1
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure R.Gleasure@ucc.ie www.robgleasure.com
2
IS2803 Today’s lecture What is PHP? PHP basic syntax Setting up XAMPP, a PHP development environment
3
What is PHP? PHP is a HTML-embedded server-side scripting language Like JavaScript, PHP is basically a sophisticated programming language, operating within the broader Web 2.0 architecture In particular, PHP is often used with MySQL to create, access, and manipulate database records
4
What’s PHP used for? ‘Server-side’ means the code gets executed on the server (where the website is stored), rather than the client machine (where the browser is accessing it from) This server-side nature of PHP means that, unlike JavaScript, it is generally used for anything which requires accessing or manipulating files stored on the server Why would this be the case? What issues would arise if client-side JavaScript were used for manipulating files stored on a host machine? What issues would arise if server-side PHP were used for manipulating dynamic content on each browser?
5
PHP Webpages All HTML pages including some PHP have the extension.php, rather than.html or.xhtml This is because, unlike HTML pages, PHP pages are not simply transferred from one location to another – they are files which must be processed to create the HTML sent to a client machine This distinction is important, as it makes developing PHP pages different to developing those containing only HTML/JavaScript
6
PHP Webpages Unlike JavaScript, coding in PHP isn’t as simple as adding some text into a HTML page in notepad, then opening the page in a browser Browsers can’t read.php pages (why would they be able to? PHP is written for servers after all) PHP pages need to be hosted by some server, then rendered for browsers requesting pages As a result, when we’re developing PHP Webpages, we need to host them on a server, or something which can emulate a server. For this course, we’re going to use XAMPP.
7
Serving PHP Webpages User enters URL of PHP page in their browser Server receives request and reads PHP file Server executes PHP statements Server sends user the HTML of the requested webpage (with all PHP statements removed) User receives PHP page with just the necessary HTML/CSS/ JavaScript code included Client MachineServer
8
Setting up XAMPP Go to http://download.cnet.com/XAMPP/3000-10248_4- 10703782.html and click download now Save and run the.exe file and follow the instructions for installation
9
Running XAMPP An XAMPP control panel will open in the bottom right of your screen, click ‘Start’ on the one beside Apache and ‘Allow’ if a Windows popup appears You can also open this control panel at any time by double- clicking the xampp icon in the bottom right corner of your Windows toolbar
10
Running XAMPP If it gets upset about Port 80, we’ll have to assign it a different port Click on the "Config" button for the "Apache" module, select "httpd.conf" file, then swap "Listen 80" line to "Listen 8080“, save the file and close it.
11
Running XAMPP Type ‘localhost’ into the address bar of your browser (or localhost:8080 if Port 80 caused a problem), then hit the return key You should be brought to the XAMPP welcome screen
12
Running XAMPP The ‘localhost’ or ‘localhost:8080’ domain is where you will run your PHP pages from but you will need to store these PHP pages in a specific folder With Apache still running, go into your C drive and open the folder called ‘xampp’ (this may be different if you chose to install xampp into a different location) Go into ‘htdocs’ and you will see there are a number of files already in there
13
Running XAMPP Create a new file in notepad/notepad++ called helloWorld.php and save it in the htdocs folder Save the following text in the helloWorld.php file <?php $my_variable = 'Hello world. '; echo $my_variable; ?>
14
Running XAMPP If you try and open helloWorld.php in a browser from here (e.g. by left-clicking on it and selecting ‘open with’), the PHP will not be processed properly and the page will display bits of the actual PHP code give this a go and see Instead we need to open it in a browser within the localhost domain, i.e. type in ‘localhost/helloWorld.php’ (or ‘localhost:8080/helloWorld.php’) in the address bar of a browser
15
Running XAMPP This is how we will practice developing php in IS2815. We will place our.php pages in the htdocs folder, then view them through the localhost domain
16
Why PHP? PHP executes very quickly PHP has native connections to many database types PHP is free, and you can download it at http://www.php.net/http://www.php.net/ PHP works on LINUX/UNIX, Windows, Mac OS, etc. It’s open source so there is plenty of support available when you run into trouble
17
The PHP syntax Much of the syntax of PHP is very similar to that of C, Java and Perl but it also has a couple of unique aspects floating around We can place a PHP script anywhere in our document A PHP script always starts with the tag (this is different to JavaScript, which just used the HTML tag)
18
The PHP syntax An example of what PHP looks like My First PHP Script <?php echo "Hello World!”; ?>
19
The PHP syntax PHP revolves around variables, loops, conditions, and functions Like JavaScript, each statement ends in a semicolon e.g. echo ‘Hello World’; Also, like JavaScript (and HTML), the amount of space between statements doesn’t matter e.g. echo ‘Hello World’; is the same as echo ‘Hello World’;
20
Variables in PHP As we saw in JavaScript, a variable can be used to store a value Variable names in PHP always begin with a dollar sign (i.e. $), after which follows a letter or underscore* Variable names must then contain only letters, numbers and underscores e.g. $variable1, $my_var, $_1234, and $AbC234 are fine but variable1, $my var, $1234, and $Ab%23& are not… * A really common mistake made when coding in multiple languages is forgetting the dollar sign after the variable has been declared
21
Variables in PHP You declare a variable simply by stating it on a single line $my_variable; You can also declare the variable and give it a value immediately $my_variable = ‘Hello world’; The same syntax can also be used to assign or change the value later on e.g. $my_variable = ‘some other text’;
22
Variables in PHP If we return to our previous example My First PHP Script <?php $my_variable = 'Hello world. '; echo $my_variable; $my_variable = 'Hello again. '; echo $my_variable; ?>
23
Variables in PHP vs JavaScript There’s one annoying thing to remember about variables that’s different in PHP to JavaScript, namely that of variable scope We remember from JavaScript that variables created inside a function are destroyed when that function ends, e.g. var x; function myFunction () { x = 1; var y = 2; } myFunction () ; document.write(x); // this will work just fine document.write(y); // this won't work because y has been destroyed
24
Variables in PHP The same thing happens in PHP, however it also goes the other way i.e. Variables created outside some function, then referenced within that function, must be explicitly referred to as global <?php $x = 0;// this variable is created outside any functions, i.e. is 'global' function myFunction () { $x = 1; /* this makes a new $x, as the $x already created is out of scope */ $y = 2; } myFunction () ; echo $x; // this will print out 0 echo $y; // this will create a new null reference $y and print that ?>
25
Variables in PHP In order to make this work, we must add <?php $x = 0; function myFunction () { global $x = 1; $y = 2; } myFunction () ; echo $x; // this will now print out 1 echo $y; // this still won't work… ?>
26
Variables in PHP We can also request for variables created inside functions not to be deleted when that function is completed by declaring them as static <?php $x = 0; function myFunction () { global $x = 1; static $y = 2; } myFunction () ; echo $x; // this will now print out 1 echo $y; // this still won't work (we'll come back to why)… ?>
27
Want to read more? Links and references Some PHP tutorials http://www.php.net/ http://www.tizag.com/phpT/ http://www.w3schools.com/php
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.