PHP: The Basics CISC 282. October 18, Approach Thus Far

Similar documents
Lecture 12. PHP. cp476 PHP

JavaScript: The Basics

PHP by Pearson Education, Inc. All Rights Reserved.

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Server side basics CS380

PHP 1. Introduction Temasek Polytechnic

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

PHP. Interactive Web Systems

What is PHP? [1] Figure 1 [1]

COMP284 Scripting Languages Lecture 9: PHP (Part 1) Handouts

Chapter 4: Basic C Operators

PHPoC vs PHP > Overview. Overview

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Operators. Java operators are classified into three categories:

CSC Web Programming. Introduction to JavaScript

CHIL CSS HTML Integrated Language

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 11 Introduction to PHP

PHP 5 Introduction. What You Should Already Know. What is PHP? What is a PHP File? What Can PHP Do? Why PHP?

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CHAD Language Reference Manual

JavaScript: More Syntax

PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

Expr Language Reference

B. V. Patel Institute of BMC & IT 2014

JavaScript CS 4640 Programming Languages for Web Applications

Java Primer 1: Types, Classes and Operators

JavaScript CS 4640 Programming Languages for Web Applications

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Server side basics CSC 210

Web Scripting using PHP

COMS 469: Interactive Media II

Chapter 2: Functions and Control Structures

Lecture 7 PHP Basics. Web Engineering CC 552

Expressions & Assignment Statements

CGS 3066: Spring 2015 JavaScript Reference

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

C++ Programming: From Problem Analysis to Program Design, Third Edition

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

COMP284 Scripting Languages Lecture 10: PHP (Part 2) Handouts

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

JavaScript I Language Basics

Such JavaScript Very Wow

Types and Expressions. Chapter 3

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Declaration and Memory

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

CSCB20 Week 8. Introduction to Database and Web Application Programming. Anna Bretscher* Winter 2017

JavaScript: Introduction, Types

Lecture 3. More About C

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Language Reference Manual simplicity

Typescript on LLVM Language Reference Manual

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

XQ: An XML Query Language Language Reference Manual

Chapter 2. Lexical Elements & Operators

Programming for the Web with PHP

Full file at

Operators And Expressions

Chapter 7. Expressions and Assignment Statements

PHPoC Language Reference > Overview. Overview

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Pace University. Fundamental Concepts of CS121 1

DC71 INTERNET APPLICATIONS JUNE 2013

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

CSI33 Data Structures

Engineering Problem Solving with C++, Etter/Ingber

Web Scripting using PHP

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

C-LANGUAGE CURRICULAM

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2: Using Data

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

PHP INTERVIEW QUESTION-ANSWERS

EL2310 Scientific Programming

Basic PHP Lecture 17

Expressions and Assignment Statements

COMP519 Practical 15 PHP (1)

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

ME 461 C review Session Fall 2009 S. Keres

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Objectives. In this chapter, you will:

Chapter 7. Expressions and Assignment Statements ISBN

Introduction to C. Systems Programming Concepts

Full file at C How to Program, 6/e Multiple Choice Test Bank

Chapter 17. Fundamental Concepts Expressed in JavaScript

Programming Language Basics

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

PHP. Introduction. PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Transcription:

PHP: The Basics CISC 282 October 18, 2017 Approach Thus Far User requests a webpage (.html) Server finds the file(s) and transmits the information Browser receives the information and displays it HTML, CSS, and/or JavaScript Without JavaScript, a website is entirely static Server returns the same information User sees the same content 2

A Different Approach User requests a webpage (.php) Server finds the file and executes it The file is in fact a program Program creates the information for transmission Browser receives the information and displays it HTML, CSS, and/or JavaScript This server-side scripting produces dynamic content Server creates different information User sees different content 3 PHP Primarily a procedural programming language Created by Rasmus Lerdorf in 1995 Adopted and augmented by other developers It is open-source Acronym represents "PHP Hypertext Processor" Initially meant "Personal Home Page" Very popular and widely available Website is www.php.net Complete documentation about the language 4

PHP vs. JavaScript Some similarities Interpreted languages Relaxed syntax Untyped variables Some differences Interpreted server-side JavaScript is interpreted in the browser You'll need to upload to the CISC 282 server for testing Embedded in HTML content Considered bad practice in JavaScript Errors are displayed as output (or lack thereof) in HTML JavaScript errors are more silent 5 Basic PHP Embedding HTML Source <?php PHP code?> HTML Source filename.php Opening tag Closing tag Interpreted and transmitted PHP block /filename.php All HTML content web browser 6

PHP Blocks Several blocks can be embedded in the same file When a browser requests a.php file The server applies the PHP preprocessor All HTML content is ignored All PHP code is interpreted Each PHP block is replaced by its output The server then transmits the results Can't see embedded code using "View Page Source" Can't see it in the browser inspector either 7 Coding Fundamentals End all expressions with a semi-colon Not optional as in JavaScript Output is produced with echo or print statements echo "output" or echo 'output' Functions are called in the usual manner functionname(arg1,,argn); Comments are designated in three different ways # and // for single lines /* */ for several lines 8

Fundamental Types Type Examples string "in double quotes", 'in single quotes', int 1, 3, 5, 7, 11, float 2.15, 0.3, boolean TRUE, FALSE (case insensitive) array [1, 3, 5, 7], object new classname(), NULL NULL 9 Working with Types Can use casting to convert from one type to another float int: rounded down string int/float: the integer/float value in the prefix All characters in the postfix are ignored 0 if there's no integer/float prefix Casting has precedence over many operators Use parentheses to ensure the correct result There are several functions that test type gettype(arg) returns a string e.g., gettype("3.1") = "string"; is_typename(arg) returns a boolean e.g., is_int(4) = TRUE and is_string(4) = FALSE; 10

Arithmetic Operators +, -, *, / and % Follow typical rules of precedence Automatically switch between types Mixing ints and floats always yields a float Division with ints may yield a float Constants M_PI: M_E: e M_LN2: log e 2 11 Some Arithmetic Functions abs(x) Function ceil(x), floor(x) min(x 1,, x N ), max(x 1,, x N ) Result The absolute value of x The value of x rounded up/down The smallest/largest value in the given list of arguments rand(x, y) A random integer in [x, y] sqrt(x) The square root of x 12

Variables Declared using the syntax $varname = varvalue; Not typed Can store a value of any variety What if you accidentally use an undeclared variable? A warning message is generated An empty value is substituted for the default (NULL) 0, 0.0, "", etc. Basic assignment operator is = +=, =, *=, /= and %= also perform the associated operation ++ and increment and decrement.= also performs string concatenation 13 Strings Enclosed in either single- or double-quotes Can access characters like elements in an array e.g., $sample = "a string" $sample[0] "a" and $sample[3] "t" Strings are concatenated with the. operator e.g., "a"."string" "astring" PHP automatically converts between types for + and. e.g., 2 + 4. "6" + 8 74 Use the typical sequences for special characters \\, \n, etc. 14

Some String Functions Function strlen(astring) trim(astring), ltrim(astring), rtrim(astring) strtoupper(astring), strtolower(astring) ord(achar), chr(anint) printf(astring) Result The number of characters in astring The contents of astring with the whitespace removed on one or both sides The contents of astring in upper/lowercase characters The corresponding ASCII/character value for the given parameter The content of astring formatted as printf would in C 15 Interpreted Strings A string that uses the values of variables e.g., "You attend $uni" "You attend Queen's" Double-quotes are required Single-quoted strings are not interpreted in this manner Enclose variables in braces to place within a word e.g., "You attend {$uni}u" "You attend Queen'sU" 16

Using Logic Boolean values are TRUE and FALSE In non-logical expressions TRUE evaluates to 1 FALSE evaluates to 0 or an empty string e.g., 1+TRUE 2 and 1.FALSE "1" Several values are considered falsey 0 and 0.0 "0" and an empty string NULL and undefined variables (by extension) An empty array All other values are considered truthy 17 Logical Operators Operator Description!=, == (In)equality of values!===,=== (In)equality of values and types <, <= Less than (or equal to) >, >= Greater than (or equal to) &&,,! AND, OR and NOT 18

if Statements if (condition) { } elseif (condition) { } else { } or if (condition): elseif (condition): else: endif; 19