[ Pobierz całość w formacie PDF ]
.When we print $orignum, we find that its value is still 10.By default, variablespassed to functions are passed by value.In other words, local copies of the valuesof the variables are made.It is possible to pass arguments to functions by reference.This means that areference to the variable is manipulated by the function rather than a copy of thevariable's value.Any changes made to an argument in these cases will change thevalue of the original variable.You can pass an argument by reference by adding anampersand to the variable name in either the function call or the function definition.Listings 6.14 and 6.15 show each technique in turn.Listing 6.14: Using a Function Call to Pass an Argument to a Function byReference1: 1042:3: Listing 6.144:5:6:15:16:Listing 6.15: Using a Function Definition to Pass an Argument to a Functionby Reference1:2:3: Listing 6.154:5:6:15:16:On the whole, it makes more sense to add the ampersand to the function definition.In this way, you can be sure that the function behaves consistently from call to call.SummaryIn this hour, you learned about functions and how to deploy them.You learned howto define and pass arguments to a function.You learned how to use the global andstatic statements.You learned how to pass references to functions and how tocreate default values for function arguments.Q&AQ Apart from the global keyword, is there any way that a function canaccess and change global variables?A You can also access global variables anywhere in your scripts with a built-inassociative array called $GLOBALS.To access a global variable called $test within afunction, you could reference it as $GLOBALS[test].You can learn more aboutassociative arrays in the next hour.You can also change global variables from within a function if it has been passed inby reference.Q Can you include a function call within a string, as you can with a variable?A No.You must call functions outside quotation marks.WorkshopThe Workshop provides quiz questions to help you solidify your understanding of thematerial covered.Try to understand the quiz answers before continuing to the nexthour's lesson.Quiz answers are provided in Appendix A. 106QuizTrue or False: If a function doesn't require an argument, you can omit theparentheses in the function call.How do you return a value from a function?What would the following code fragment print to the browser?$number = 50;function tenTimes(){$number = $number * 10;}tenTimes();print $number;What would the following code fragment print to the browser?$number = 50;function tenTimes(){global $number;$number = $number * 10;}tenTimes();print $number;What would the following code fragment print to the browser?$number = 50;function tenTimes( $n ){$n = $n * 10;}tenTimes( $number );print $number; 107What would the following code fragment print to the browser?$number = 50;function tenTimes( &$n ){$n = $n * 10;}tenTimes( $number );print $number;ActivityCreate a function that accepts four string variables and returns a string thatcontains an HTML table element, enclosing each of the variables in its own cell. 108Hour 7: ArraysOverviewArrays, and the tools to manipulate them, greatly enhance the scope and flexibilityof PHP4 scripts.After you've mastered arrays, you will be able to store and organizecomplex data structures.This hour introduces arrays and some of the functions that help you work with them.In this hour, you will learnWhat arrays are and how to create themHow to access data from and about arraysHow to access and sort the data contained in arraysWhat Is an Array?You already know that a variable is a "bucket" in which you can temporarily store avalue.By using variables, you can create a script that stores, processes, andoutputs different information every time it is run.Unfortunately, you can only storeone value at a time in a variable.Arrays are special variables that allow you toovercome this limitation.An array allows you to store as many values as you wantin the same variable.Each value is indexed within the array by a number or a string.If a variable is a bucket, you can think of an array as a filing cabinet a singlecontainer that can store many discrete items.NEW An array is a list variable.That is, a variable that contains multipleTERM elements indexed by numbers or strings.It enables you to store,order, and access many values under one name.Of course, if you have five values to store, you could always define five variables.So,why use an array rather than a variable? First, an array is flexible [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • swpc.opx.pl
  •