270 likes | 276 Views
Discover the simplicity, efficiency, and security of PHP, a server-side scripting language that is widely used for websites. Learn about its syntax, popularity, and characteristics, and get started with your first PHP script.
E N D
PHP? • PHP started out as a small open source project that evolved as more and more people found out how useful it was. RasmusLerdorf unleashed the first version of PHP way back in 1994. • PHP: Hypertext Preprocessor • PHP Syntax is C-Like • Server side scripting language that is embedded in HTML • PHP supports a large number of major protocols
Characteristics of PHP • Simplicity • Efficiency • Security • Flexibility • Familiarity
Popularity of PHP • The PYPL PopularitY of Programming Language Index is created by analyzing how often language tutorials are searched on Google.
Escaping in PHP • Canonical PHP tags: • <?php...?> • Short-open (SGML-style) tags: • <?...?> • Set the short_open_tag setting in your php.ini file to on. This option must be disabled to parse XML with PHP because the same syntax is used for XML tags. • ASP-style tags • <%...%> • HTML script tags: • <script language="PHP">...</script>
Exercise • Create your first PHP script. Write the string “Hello world..is it me you’re looking for?”
Commenting PHP code • Single-line comments • #This is a comment • //This is a comment • Multi-lines comments • /* This is a comment for multiple lines */
Things to Remember • PHP is whitespace insensitive • PHP is case sensitive • Statements are expressions terminated by semicolons • Expressions are combinations of tokens • Braces make blocks
Variables • All variables in PHP are denoted with a leading dollar sign ($). • The value of a variable is the value of its most recent assignment. • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right. • Variables can, but do not need, to be declared before assignment. • Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used to store a number or a string of characters. • Variables used before they are assigned have default values. • PHP does a good job of automatically converting types from one to another when necessary. • PHP variables are Perl-like.
PHP Data Types • Integer • Doubles • Booleans • NULL • String • Array • Object • Resources Simple Types Compound Types Special variables that reference to external resources
Variable Scopes • Local variables – variable declared in a function, and can only be referenced in that function.
Variable Scopes • Function parameters - are declared after the function name and inside parentheses.
Variable Scopes • Global variables – can be accessed in any part of the program. Must be declared using the keyword GLOBAL
Variable Scopes • Static variables – does not lose the value when the function exits and will still hold the value when it is called again. Uses the keyword STATIC
Variable naming • Variable names must begin with a letter or underscore character. • A variable name can consist of numbers, letters, underscores but you cannot use characters like + , - , % , ( , ) . & , etc • There is no size limit for variables.
Operator types • Arithmetic Operators
Operator types • Comparison Operators
Operator types • Logical Operators
Operator types • Assignment Operators
Operator types • Conditional Operators
Exercise • Create a variable named count and assign the number 0 • Create a