Question? Leave a message!

Introduction to PHP

Introduction to PHP 21
NicolusNorton Profile Pic
Published Date:08-07-2017
Website URL
PHP/MySQL Introduction Goal • Not to teach everything about PHP, but provide the basic knowledge • Explain code of examples • Provide some useful references PHP Basics: Introduction to PHP • a PHP file, PHP workings, running PHP.  Basic PHP syntax • variables, operators, if...else...and switch, while, do while, and for.  Some useful PHP functions  How to work with • HTML forms, cookies, files, time and date.  How to create a basic checker for user-entered dataServer-Side Dynamic Web Programming • CGI is one of the most common approaches to server-side programming  Universal support: (almost) Every server supports CGI programming. A great deal of ready-to-use CGI code. Most APIs (Application Programming Interfaces) also allow CGI programming.  Choice of languages: CGI is extremely general, so that programs may be written in nearly any language. Perl is by far the most popular, with the result that many people think that CGI means Perl. But C, C++, Ruby, and Python are also used for CGI programming.  Drawbacks: A separate process is run every time the script is requested. A distinction is made between HTML pages and code.• Other server-side alternatives try to avoid the drawbacks  Server-Side Includes (SSI): Code is embedded in HTML pages, and evaluated on the server while the pages are being served. Add dynamically generated content to an existing HTML page, without having to serve the entire page via a CGI program.  Active Server Pages (ASP, Microsoft) : The ASP engine is integrated into the web server so it does not require an additional process. It allows programmers to mix code within HTML pages instead of writing separate programs. (Drawback(?) Must be run on a server using Microsoft server software.)  Java Servlets (Sun): As CGI scripts, they are code that creates documents. These must be compiled as classes which are dynamically loaded by the web server when they are run.  Java Server Pages (JSP): Like ASP, another technology that allows developers to embed Java in web pages.PHP • developed in 1995 by Rasmus Lerdorf (member of the Apache Group)  originally designed as a tool for tracking visitors at Lerdorf's Web site  within 2 years, widely used in conjunction with the Apache server  developed into full-featured, scripting language for server-side programming  free, open-source  server plug-ins exist for various servers  now fully integrated to work with mySQL databases • PHP is similar to JavaScript, only it’s a server-side language  PHP code is embedded in HTML using tags  when a page request arrives, the server recognizes PHP content via the file extension (.php or .phtml)  the server executes the PHP code, substitutes output into the HTML page  the resulting page is then downloaded to the client  user never sees the PHP code, only the output in the pageWhat do You Need? • Our server supports PHP • You don't need to do anything special • You don't need to compile anything or install any extra tools • Create some .php files in your web directory - and the server will parse them for you. Slightly different rules apply when dealing with an SQL database (as will be explained when we get to that point). • Most servers support PHP • Download PHP for free here: • Download MySQL for free here: • Download Apache for free here: (Note: All of this is already present on the CS servers, so you need not do any installation yourself to utilize PHP on our machines.)What is PHP? • PHP == ‘Hypertext Preprocessor’ • Open-source, server-side scripting language • Used to generate dynamic web-pages • PHP scripts reside between reserved PHP tags • This allows the programmer to embed PHP scripts within HTML pages • The acronym PHP means (in a slightly recursive definition)  PHP: Hypertext PreprocessorWhat is PHP (cont’d) • Interpreted language, scripts are parsed at run-time rather than compiled beforehand • Executed on the server-side • Source-code not visible by client •‘View Source’ in browsers does not display the PHP code • Various built-in functions allow for fast development • Compatible with many popular databasesWhat does PHP code look like? • Structurally similar to C/C++ • Supports procedural and object-oriented paradigm (to some degree) • All PHP statements end with a semi-colon • Each PHP script must be enclosed in the reserved PHP tag ?php … ?Comments in PHP • Standard C, C++, and shell comment symbols // C++ and Java-style comment Shell-style comments / C-style comments These can span multiple lines /Variables in PHP • PHP variables must begin with a “” sign • Case-sensitive (Foo = foo = fOo) • Global and locally-scoped variables • Global variables can be used anywhere • Local variables restricted to a function or class • Certain variable names reserved by PHP • Form variables (_POST, _GET) • Server variables (_SERVER) • Etc.Constants A constant is an identifier (name) for a simple value. A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. ?php // Valid constant names define("FOO", "something"); define("FOO2", "something else"); define("FOO_BAR", "something more"); You can access // Invalid constant names (they shouldn‟t start constants anywhere // with a number) in your script define("2FOO", "something"); without regard to scope. // This is valid, but should be avoided: // PHP may one day provide a “magical” constant // that will break your script define("__FOO__", "something"); ?Operators Example Is the same as • Arithmetic Operators: +, -, ,/ , %, ++, x+=y x=x+y x-=y x=x-y • Assignment Operators: =, +=, -=, =, /=, %= x=y x=xy x/=y x=x/y x%=y x=x%y • Comparison Operators: ==, =, , , =, = • Logical Operators: &&, , • String Operators: . and .= (for string concatenation) a = "Hello "; b = a . "World"; // now b contains "Hello World" a = "Hello "; a .= "World";Variable usage ?php foo = 25; // Numerical variable bar = “Hello”; // String variable foo = (foo 7); // Multiplies foo by 7 bar = (bar 7); // Invalid expression ?Basic PHP syntax A PHP scripting block always starts with ?php and ends with ?. A PHP scripting block can be placed (almost) anywhere in an HTML document. html print and echo hello.php headtitleHello World/title/head for output body pThis is going to be ignored by the PHP interpreter./p ?php echo „pWhile this is going to be parsed./p„; ? a semicolon (;) at the end of each pThis will also be ignored by the PHP preprocessor./p statement ?php print(„pHello and welcome to imy/i page/p'); ? ?php // for a single-line comment //This is a comment / and / for a large / This is comment block. a comment block / ? /body /html The server executes the print and echo statements, substitutes output.Scalars All variables in PHP start with a sign symbol. A variable's type is determined by the context in which that variable is used (i.e. there is no strong-typing in PHP). htmlhead/head scalars.php body p ?php foo = true; if (foo) echo "It is TRUE br / \n"; Four scalar types: txt='1234'; echo "txt br / \n"; a = 1234; echo "a br / \n"; boolean a = -123; true or false echo "a br / \n"; a = 1.234; integer, echo "a br / \n"; a = 1.2e3; float, echo "a br / \n"; floating point numbers a = 7E-10; echo "a br / \n"; string echo 'Arnold once said: "I\'ll be back"', "br / \n"; beer = 'Heineken'; single quoted echo "beer's taste is great br / \n"; double quoted str = EOD Example of string spanning multiple lines using “heredoc” syntax. EOD; echo str; ? /p /body /htmlEcho • The PHP command „echo‟ is used to output the parameters passed to it • The typical usage for this is to send data to the client‟s web-browser • Syntax • void echo (string arg1 , string argn...) • In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual functionEcho example ?php foo = 25; // Numerical variable bar = “Hello”; // String variable echo bar; // Outputs Hello echo foo,bar; // Outputs 25Hello echo “5x5=”,foo; // Outputs 5x5=25 echo “5x5=foo”; // Outputs 5x5=25 echo „5x5=foo‟; // Outputs 5x5=foo ? • Notice how echo „5x5=foo‟ outputs foo rather than replacing it with 25 • Strings in single quotes („ ‟) are not interpreted or evaluated by PHP • This is true for both variables and character escape-sequences (such as “\n” or “\\”)