Strings, output, quotes and comments

Slides:



Advertisements
Similar presentations
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
IT151: Introduction to Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Introduction to scripting
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP : Hypertext Preprocessor
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Writing Web Pages By Shyam Gurram. Agenda Writing Web Pages Delimiting PHP Program Units. Displaying Output to Web Pages Putting Comments in PHP Programs.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 PHP Introduction Chapter 1. Syntax and language constructs.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Comments in PHP In PHP, we use // to make a singleline comment or /* and */ to make a large comment block. Comment is a part of your PHP code that will.
CS346 Javascript -3 Module 3 JavaScript Variables.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Python Let’s get started!.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP: The Basics.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Introduction to PHP and MySQL (Creating Database-Driven Websites)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 2 Variables.
Chapter 6 JavaScript: Introduction to Scripting
PHP (PHP: Hypertext Preprocessor)
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Intro to PHP & Variables
2.1 Parts of a C++ Program.
WEB PROGRAMMING JavaScript.
Chapter 2: Introduction to C++.
Tutorial 10: Programming with javascript
Chapter 2 Variables.
Presentation transcript:

Strings, output, quotes and comments PHP Intro Strings, output, quotes and comments

Basic Syntax PHP is an HTML-embedded scripting language Start with a basic HTML file To add PHP code, place it within PHP tags: <?php ?>

Example <!DOCTYPE html> <html lang="en"> <head> <!-- File name must have php extension: first.php --> <title>First Page</title> <meta charset="utf-8"> </head> <body> Here is some text using plain HTML <br> <?php echo 'Hello PHP World'; ?> </body> </html>

PHP comments Three types: # or // for single-line or end-of-line comments /* longer comments, spanning more than one line require this style of comment. */

PHP comments Why bother? A reminder of what the script does (especially helpful for those who need to edit your code after you are long gone!) As a placeholder for code to be filled in later. To disable a section of code for testing and debugging purposes.

PHP variables Syntax rules The six PHP data types PHP statements end with a semicolon. PHP ignores extra whitespace in statements. The six PHP data types integer double Boolean string array object

Data types Integer values (whole numbers) 15 // an integer -21 // a negative integer Double values (numbers with decimal positions) 21.5 // a floating-point value -124.82 // a negative floating-point value The two Boolean values true // equivalent to true, yes, or on false // equivalent to false, no, or off String values 'Ray Harris' // a string with single quotes "Ray Harris" // a string with double quotes '' // an empty string null // a NULL value

Rules for variable names Variable names begin with $ Data types are not declared: they are determined by the PHP interpreter Variable names are case-sensitive Variable names can contain letters, numbers, and underscores. Variable names can’t contain special characters. Variable names can’t begin with a digit or two underscores. Variable names can’t use names that are reserved by PHP such as the variable named $this which is reserved for use with objects.

Conventions for variable names Use meaningful names: $lastName, $userID, etc. ($s, $x, etc. don't help describe the variable and make your code hard to read) Be consistent: use camel case, or underscores, to combine words used as identifiers, typically using lower case for the first letter and then stick with that pattern throughout Variable names are case-sensitive: $first_Name is not the same variable as $first_name

Sending DATA to the browser echo and print both send output to the browser technically they aren’t “functions” in the typical programming sense but they are often referred to as such echo can accept one or more string variables / print can only accept one parentheses are optional echo does not return a value / print returns a value of 1

PHP quotes A string must begin and end with the same type of quote Single quotes are treated as literal strings Double quotes are interpreted: the interpreter looks inside the quotes to determine if there are variables, and if so, the value of the variable is used. If the same type of quote appears in the string, it can be escaped with \

String examples $first_name = 'Bob'; $last_name = 'Roberts'; Use single quotes to improve PHP efficiency $first_name = 'Bob'; $last_name = 'Roberts'; Assign NULL values and empty strings $address2 = '';// an empty string using two single quotes $address2 = null; // a NULL value Use double quotes for variable substitution $name = "Name: $first_name"; // Name: Bob $name = "$first_name $last_name"; // Bob Roberts

String examples (cont.) Mix single and double quotes for special purposes $last_name = "O'Brien"; // O'Brien $line = 'She said, "Hi."'; // She said, "Hi." Or use the escape character $last_name = 'O\'Brien'; // O'Brien $line = 'She said, \'Hi.\' '; // She said, 'Hi.'

Using echo with strings <!DOCTYPE html> <html lang="en"> <head><meta charset="utf-8"><title>Strings</title></head> <body> <?php // Create the variables: $first_name = 'Haruki'; $last_name = 'Murakami'; $book = 'Kafka on the Shore'; // Print the values: echo "<p>The book <em>$book</em> was written by $first_name $last_name.</p>"; ?> </body> </html>

Concatenation Concatenation is like addition for strings It is the process of appending one string to another The period ( . ) is the concatenation operator for PHP $city = 'Wilmington'; $state = 'NC'; $locale = $city . $state; //what is the result? Improvement: $locale = $city . ', ' . $state;

Escape sequences Many characters have meanings in the PHP language, $ for example. When you want the character to be interpreted literally, it must be escaped with the \ character. Characters requiring an escape sequence: Code Meaning \" Double quotation mark \' Single quotation mark \\ Backslash \n Newline \r Carriage return \t Tab \$ Dollar sign

Basic debugging Don't forget to run PHP scripts through a URL Make sure the file name has the .php extension A blank page means there is a syntax error somewhere. Check the HTML source code: run it through a validator. It may point you to the problem.

Debugging (cont.) Don't put comments on the first line of a page. Use echo statements for testing purposes. Use comments to bypass sections of code for testing. This helps to isolate the problem. If you are editing locally, be sure to save and re-upload the revised file before testing.

Quiz yourself: Are variable names case-sensitive or case-insensitive? What are the three types of comment syntaxes in PHP? // B. # C. /* */ Which type of quotes treat the string as a literal value? Single quotes What is the purpose of the escape character, \ ? It means that the following character should be treated as it's character code and not as it's used in the language