🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

CP476 Internet Computing-week-1-2.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

CP476 Internet Computing Week 1 – 2 PHP - Data types and Variables Shaun Gao, Ph.D., P.Eng. Agenda PHP processing Output methods Data types Integers and Floating-point numbers Booleans Strings Array, object Resource, NULL Data type conversion Variables https://www.w3schools.com/php/default.asp PHP p...

CP476 Internet Computing Week 1 – 2 PHP - Data types and Variables Shaun Gao, Ph.D., P.Eng. Agenda PHP processing Output methods Data types Integers and Floating-point numbers Booleans Strings Array, object Resource, NULL Data type conversion Variables https://www.w3schools.com/php/default.asp PHP processing PHP code is embedded into HTML pages using tags () Processing proceeds as follows: The web server receives a client request The web server recognizes that the client request is for a HTML document containing PHP code The server executes the PHP code, substitutes output into the HTML document, the resulting page is then send to the client The client never sees the PHP code, only the HTML document that is produced PHP output methods: echo and print With PHP, there are two basic ways to get output: echo and print Echo: The echo statement can be used with or without parentheses: echo or echo() such that, echo $var1; or echo($var1); Print: The print statement can be used with or without parentheses: print or print() such that, print $var1; or print($var1); Example: echo "I'm about to learn PHP! \n"; Print "I'm about to learn PHP too! \n"; echo ("I'm about to learn PHP! \n"); https://onlinephp.io/ PHP basics – data types PHP has eight datatypes before version 8 Four primitive types: bool - Booleans int - integers float - floating-point numbers string - strings Two compound types: array - arrays object - objects Two special types: resource NULL mixed is a pseudo type added in PHP 8 PHP basics– strings PHP distinguishes between single-quoted strings and double-quoted strings. PHP basics - array What is an array? Array is a data type that can hold more than one value at a time. How to create an array? array() function In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index $cars = array("Volvo", "BMW", "Toyota"); $cars Associative arrays - Arrays with named keys $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Multidimensional arrays - Arrays containing one or more arrays PHP basics – array – examples. Example 1: Example 2: https://sandbox.onlinephpfunctions.com/ PHP basics – array – examples. Example 3: 2 dimensional array https://onlinephp.io/ PHP basics – objects. object – An object is a data type which stores not only data but also information on how to process that data. Unlike the other data types in PHP, an object type must be explicitly declared. A class is a template for objects, and an object is an instance of a class. Converting to object If an object is converted to another object, it is not modified. If arrays are converted to an object with properties named by keys, and corresponding values. For any other value, a member variable named scalar will contain the value. Example: $object = (object) $array; PHP basics – resource Resource A resource is a special data type that refers to an external resource. Resources are created and used by special functions. Examples of Resource Types PHP basics – NULL NULL The special null value represents a variable with no value. null is the only possible value of type null. A variable is considered to be null if: it has been assigned to null. it has not been set to any value yet (empty). it has been unset(). Example: $a = array(); echo $a == null; Question? (PHP NULL differs from NULL in C/C++) PHP predefined math functions PHP supports a wide range of pre-defined mathematical functions abs(number) ceil(number) floor(number) round(number [,prec,mode]) log(number [,base]) rand(min,max) sqrt(number) absolute value round fractions up round fractions down round fractions logarithm generate an integer random number square root PHP provides pre-defined number constants including M_PI NAN INF 3.14159265358979323846 `not a number' `infinity' PHP – NAN, INF, Booleans The constants NAN and INF are used as return values for some applications of mathematical functions that do not return a number log(0) returns -INF (negative `infinity') sqrt(-1) returns NAN (`not a number’) PHP has a Boolean datatype with constants TRUE and FALSE (case insensitive) PHP offers the same Boolean operators as C++, JavaScript && (conjunction) || (disjunction) Online demos PHP – type conversion to Boolean When converting to Boolean, the following values are considered FALSE: the Boolean the integer the float the string the empty string FALSE 0 (zero) 0.0 (zero) '0' (but not 0.0 nor '00') ‘’” Every other value is considered TRUE (including any resource) When converting a Boolean to a string, TRUE becomes "1" FALSE becomes "" PHP – type conversion in general Type casting is to utilize one data type variable into the different data type. Type casting is the explicit conversion of data type. Example: $string_var = "string value for php type"; $int_var = (array)$string_var; var_dump($int_var); PHP – variables All PHP variable names start with $ followed by a PHP identifier A PHP identifier consists of letters, digits, and underscores, but cannot start with a digit. PHP identifiers (variables) are case sensitive In PHP, a variable does not have to be declared before it can be used A variable also does not have to be initialised before it can be used, although initialisation is a good idea Uninitialized variables have a default value: PHP - variable Assignments PHP uses the equality sign = for assignments. Example: $b = ($a = 0) + 1; PHP also supports the standard binary assignment operators: PHP - constants Use the keyword “define” to define a constant or an array of constants. define(string, expr [, case_insensitive]) define ("PI" ,3.14159); define (" ANIMALS " ,[" bird "," cat"," dog" ]); The constant is globally accessible within a script Online demo: define ("PI" ,3.14159); Echo PI; Summary PHP processing Output methods Data types Integers and Floating-point numbers Booleans Strings Array, object Resource, NULL Data type conversion – not recommended Variables Announcement How about environment setup? Visual Studio Code as an IDE from week 2-3 Please send me an email with your group members’ full names by the end of January 2024. https://www.w3schools.com/php/default.asp

Use Quizgecko on...
Browser
Browser