Basic Scripting & Variables Yasar Hussain Malik - NISTE.

Slides:



Advertisements
Similar presentations
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
Advertisements

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
PHP Introduction.
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
String Escape Sequences
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Objectives You should be able to describe: Data Types
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
INTERNET APPLICATION DEVELOPMENT For More visit:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Data types, Literals (constants) and Variables Data types specify what kind of data, such as numbers and characters, can be stored and manipulated within.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
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.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
CS346 Javascript -3 Module 3 JavaScript Variables.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
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 PHP Introduction Chapter 1. Syntax and language constructs.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Tharith Sriv.  JavaScript is a simple programming language that can be written directly into HTML documents to allow for increased interactivity with.
Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
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,
Introduction to PHP and MySQL (Creating Database-Driven Websites)
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
JavaScript, Sixth Edition
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Scope, Objects, Strings, Numbers
Basics of ‘C’.
PHP.
JavaScript What is JavaScript? What can JavaScript do?
JavaScript What is JavaScript? What can JavaScript do?
PHP an introduction.
Presentation transcript:

Basic Scripting & Variables Yasar Hussain Malik - NISTE

 Whitespace  Escape sequences  Brief introduction to variable types  Data Types  Variable Functions  Variable variables  Pre-set variables  References (pointer)  Constants  Mathematical Constants  Operators  The Ternary Operator  Operator precedence and associability  Variable Scope

 Spaces, tabs, and new lines in between statements have no effect on how the code is executed. For example, this next script is the same as the previous script, even though it is laid out quite different:   It is generally recommended that you use whitespace to separate your code into clear blocks, so that it can almost be understood simply by visually inspecting the layout. like  $name = "Paul";  print "Your name is $name\n"; $name2 = $name;  $age = 20;

 Escape sequences consist of a backslash followed by a single character. When enclosed in double quotes, the backslash causes the interpretation of the next character to "escape" from its normal ASCII code and to represent something else. To display the escape sequences in your browser, the HTML tag can be used; otherwise, the escape sequences placed within your PHP script will not be interpreted.  \‘ Single quotation mark  \“ Double quotation  \t Tab  \n Newline  \r Return/line feed  \$ A literal dollar sign  \\ Backslash  \70 Represents the octal value  \x05 Represents the hexadecimal character

PHP supports four core data types: Integer Float (also called double) String Boolean In addition to the four core data types, there are four other special types: Null Array Object Resources To see the type of a variable use function $type=gettype(54.6);

 String literals are a row of characters enclosed in either double or single quotes. 1 The quotes must be matched.  If a string of characters is enclosed in single quotes, the characters are treated literally (each of the characters represents itself).  Double quotes do not treat all characters equally. If a string is enclosed in double quotes, most of the characters represent themselves, but dollar signs and backslashes have a special meaning  Single quotes can hide double quotes, and double quotes can hide single quotes  "This is a string“  'This is another string‘  "This is also 'a string‘“  'This is "a string"‘  "5" is a string, whereas 5 is a number.

 Boolean literals (introduced in PHP 4) are logical values that have only one of two values, true or false, both case insensitive.  You can think of the values as yes or no, on or off, or 1 or 0.  When using numeric comparison and equality operators, the value true evaluates to 1 and false evaluates to the empty

 Null NULL represents "no value," meaning "nothing," not even an empty string or zero. It is a type of NULL. An uninitialized variable contains the value NULL. A variable can be assigned the value NULL, and if a variable has been unset, it is considered to be NULL.  Resource A resource is a special variable, holding a reference to an external resource such as a database object or file handler. Resources are created and used by special functions. File and database resources are defined by the PHP interpreter and are only accessible by functions provided by the interpreter.  The gettype() Function The gettype() built-in function returns a string to identify the data type of its argument. The argument might be a variable, string, keyword, and so on. You can use the gettype() function to check whether or not a variable has been defined because if there is no value associated with the variable, the gettype() function returns NULL Format string gettype ( mixed var ) $type=gettype(54.6); // Returns "float" (e)

 To test whether a variable has a value set simply call the isset() function passing through the name of the variable as an argument. The following code example tests to see if a variable has a value or not and displays a message accordingly:  <?php  $myVariable = "hello";   if (isset($myVariable))  {  echo "It is set.";  }  else  {  echo "It is not set.";  }  ?>

 Forcing a type with type casting  PHP will automatically convert data types as necessary across the board.  If you specifically wish to override PHP's type conversion, you can perform what is called a type cast  you forcibly convert a variable of type A to type B. In PHP, type casting looks like this: 

When you declare a variable outside a function, it may not be seen in the function. When you declare a variable inside a function, it may not be seen outside the function. That feature is called variable scope.

If you find yourself setting a variable for convenience and never changing it during a script, chances are you should be using a constant. Constants are like variables except that once they are defined, they cannot be undefined or changed they are constant, as the name suggests. Unlike many other languages, constants are not faster than variables in PHP. The primary advantage to using constants is the fact that they do not have a dollar sign at the front and, therefore, are visibly different from variables. Furthermore, constants are automatically global across your entire script, unlike variables.

 You can define a constant by using the define()-function or by using the const keyword outside a class definition as of PHP Once a constant is defined, it can never be changed or undefined.  Only scalar data (Boolean, integer, float and string) can be contained in constants. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.  You can get the value of a constant by simply specifying its name. Unlike with variables, a constant should not have a$ sign. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically. Use get_defined_constants() to get a list of all defined constants

 These are the differences between constants and variables:  Constants do not have a dollar sign ($) before them;  Constants may only be defined using the define() function, not by simple assignment;define()  Constants may be defined and accessed anywhere without regard to variable scoping rules;  Constants may not be redefined or undefined once they have been set; and  Constants may only evaluate to scalar values.  Example Defining Constants 

Arithmatic OperatorDescriptionExampleResult +Additionx=2, x+24 -Subtractionx=2, 5-x3 *Multiplicationx=4, x*520 /Division15/5, 5/23, 2.5 %Modulus (division remainder)5%2, 10%8, 10%21, 2, 0 ++Incrementx=5, x++x=6 --Decrementx=5, x--x=4 Assignment OperatorExampleIs The Same As =x=y +=x+=yx=x+y -=x-=yx=x-y *=x*=yx=x*y /=x/=yx=x/y.=x.=yx=x.y %=x%=yx=x%y

Comparison Operator DescriptionExample ==is equal to5==8 returns false !=is not equal5!=8 returns true <>is not equal5<>8 returns true >is greater than5>8 returns false <is less than5<8 returns true >=is greater than or equal to5>=8 returns false <=is less than or equal to5<=8 returns true Logical Operator DescriptionExample &&and x=6 y=3 (x 1) returns true ||or x=6 y=3 (x==5 || y==5) returns false !notx=6 y=3 !(x==y) returns true

 It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false. The ternary operator is a shorthand way of doing if statements. Here's an example: 