[ Pobierz całość w formacie PDF ]
.echoKeep in mind that you can mix variables and code within double quotes.$var = this string ;echo Please print $var ;3537-4 ch06.f.qc 12/15/00 15:22 Page 149Chapter 6: PHP s Built-in Functions 149However, within single quotes the string will be treated literally:$var = this string ;echo Please print $var ;The preceding code will print Please print $var.This concept is discussed ingreater detail in Chapter 4.print versus echo.Which should you use? This is very much a matter of per-sonal preference: use whichever you think looks better on the page.There sonly one major difference between the two, and this may influence yourdecision.echo can take multiple arguments.That is, with echo, different por-tions can be separated by commas.This will work:echo this is part 1 , this is part 2 ;But this will not:print this is part 1 , this is part 2 ;PRINT FUNCTIONS NOT USED IN THIS BOOKThey didn t come up here, but these are really important to know about.PRINTF() This function outputs a string using the format specified in the descriptionof the sprintf() function.PRINT_R() This function is great for putting to productive use the time you d other-wise spend pulling your hair out.It prints the entire contents of any variable mostnotably arrays and objects to the screen.void print_r (mixed expression)We use it frequently when we re not getting the results we expect from arrays orobjects.Do not do print_r($GLOBALS).You will create an infinitely recursive loop.3537-4 ch06.f.qc 12/15/00 15:22 Page 150150 Part II: Working with PHPVAR_DUMP() This function behaves like print_r, but gives you a bit more infor-mation.void var_dump (mixed expression)In addition to printing out the contents of a variable, it includes the data type including the data type for each element in an array or object.The same cautiongiven for print_r applies to var_dump().Date/time functionsIn point of fact, dealing with PHP and MySQL as a team, you will have to get toknow two sets of date/time functions and they are quite different.This isn t thetime or place to go into MySQL s functions (see Appendix I for that).PHP stime/date functions are very well designed.DATE/TIME FUNCTIONS USED IN THE BOOKThe following are some date/time functions used in the applications in this book.DATE() You can use this function and the indicators outlined next to return thedate and time.string date (string format [, int timestamp])If you include a second argument, that value will be formatted as you prescribe.Otherwise, the current time and date will be used.The time and date the functions return are based on the time on the server.TipYou will need to make use of JavaScript to get an idea of the time on theclient s computer.Often the second argument will be the result of the mktime()function,which we discuss next.You can format the date using any of the indicators in Table 6-1.3537-4 ch06.f.qc 12/15/00 15:22 Page 151Chapter 6: PHP s Built-in Functions 151TABLE 6-1 INDICATORS FOR THE DATE( ) FUNCTIONIndicator Meaninga am or pmA AM or PMB Swatch Internet timed Day of the month, 2 digits with leading zeros; 01 to 31D Day of the week, textual, 3 letters; e.g.FriF Month, textual, long; e.g.Januaryg Hour, 12-hour format without leading zeros; 1 to 12G Hour, 24-hour format without leading zeros; 0 to 23h Hour, 12-hour format; 01 to 12H Hour, 24-hour format; 00 to 23i Minutes; 00 to 59I [capital i] 1 if Daylight Savings Time, 0 otherwisej Day of the month without leading zeros; 1 to 31l (lowercase l) Day of the week, textual, long; e.g.FridayL Boolean for whether it is a leap year; 0 or 1m Month; 01 to 12M Month, textual, 3 letters; e.g.Jann Month without leading zeros; 1 to 12s Seconds; 00 to 59S English ordinal suffix, textual, two characters; e.g.th, ndt Number of days in the given month; 28 to 31T Time-zone setting of this machine; e.g.MDTU Seconds since the epoch (midnight, January 1, 1970)w Day of the week, numeric, 0 (Sunday) to 6 (Saturday)Continued3537-4 ch06.f.qc 12/15/00 15:22 Page 152152 Part II: Working with PHPTABLE 6-1 INDICATORS FOR THE DATE( ) FUNCTION (Continued)Indicator MeaningY Year, four digits; e.g.1999y Year, two digits; e.g.99z Day of the year; 0 to 365Z Time-zone offset in seconds (-43200 to 43200)For example, if you want to print the date in the format, September 14, 20007:21 pm, this would do the trick:echo date( F d, Y g:i a );In case you re wondering about the significance of the above date: it was theexact time we wrote this portion of this chapter.MKTIME() This function is most useful for calculating valid dates.int mktime (int hour, int minute, int second, int month, int day,int year [, int is_dst])For example, say that you have a form that collects a date, maybe the currentmonth, day, and year.You want to calculate and set a due date exactly 30 daysfrom the date submitted.$year = 2000;$month = 5;$day = 24;echo date( l F d, Y , mktime(0,0,0,$month,$day+30, $year) );This will output 30 days from May 24, 2000 and will print out Friday June 23,2000 [ Pobierz całość w formacie PDF ]