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

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

Full Transcript

CP476 Internet Computing Week 3 – 1 PHP – functions Shaun Gao, Ph.D., P.Eng. Objectives Functions Defining and Calling a Function Scope of Variables Functions and HTML variable arguments Anonymous Functions PHP libraries Include/Require PHP – function A function name must start with a letter or an u...

CP476 Internet Computing Week 3 – 1 PHP – functions Shaun Gao, Ph.D., P.Eng. Objectives Functions Defining and Calling a Function Scope of Variables Functions and HTML variable arguments Anonymous Functions PHP libraries Include/Require PHP – function A function name must start with a letter or an underscore. Function names are NOT case-sensitive. function writeMsg() { echo "Hello world!"; } WritemsG();  function definition  function calling/execution Default Argument Value: function setHeight(int $minheight = 50) { echo "The height is : $minheight \n"; } setHeight(350); SETHeight(); // will use the default value of 50 Demo1: PHP – functions – variable scope Variables and Scope: PHP distinguishes the following categories of variables with respect to scope: Local variables are only valid in the part of the code in which they are introduced. variables inside a function are local: inside the curly bracket { } (PHP) variables outside any function definition are global. Global variables are valid outside of functions. Key word global is case insensitive. Global, globAL … are same Static variables are local variables within a function that retain their value between separate calls of the function Superglobal variables are built-in variables that are always available in all scopes. Demo 1-1: key word global PHP Functions and Static Variables A variable is declared to be static using the keyword static and should be combined with the assignment of an initial value (initialisation). Example: function counter () { static $count = 0; return $count++; } echo "static \$count = ",counter(),"\n"; echo "static \$count = ",counter(),"\n"; Demo2: Functions and HTML It is possible to include HTML markup in the body of a PHP function definition. "> Demo3: Functions and HTML – cont. It is possible to include HTML markup in the body of a PHP function definition. The output of the about php script execution is as follows: Demo 4: Super Global variables are associative array, you can define your own key and value. PHP Global Variables – cont. $_COOKIE A cookie is a piece of data from a web server that is stored within a web browser for web server to recognize the web users. Sending the cookie with PHP Receiving the cookie in your browser Accessing a cookie in a PHP script PHP Global Variables – cont. $_COOKIE, HTTP Cookies A cookie is a small file that the server embeds on the user's computer for identifying the user. Each time the same computer requests a page with a browser, it will send the cookie too. Then the web server will know who they are. A cookie is created with the setcookie() function. Syntax. setcookie(name, value, expire, path, domain, secure, httponly); Demo5: PHP Global Variables – cont. setcookie(name, value, expire, path, domain, secure, httponly); Only the name parameter is required. All other parameters are optional. Example:

Use Quizgecko on...
Browser
Browser