(This page has no text content)
PHP Programming A Step-by-Step Guide to Learn, in an Easy Way, the Fundamentals of PHP Programming Language 3nd Edition By Daniel Robinson Content: Part 1. Language syntax and operators Expressions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Part 2. Functions for working with data Mathematical functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Rounding functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Random numbers . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Translation into various number systems . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Minimum and maximum . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Power functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Trigonometry . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Advanced precision functions BCMath . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... GMP functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... GMP functions. GMP function values . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
GMP functions. Arithmetic . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... GMP functions. Mathematics . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... GMP functions. Binary operations . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with arrays . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Array creation . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Sorting arrays . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Array cursor . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Keys and Values . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Complex replacement in a string . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with multiple arrays . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Getting and removing part of an array . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Insert / remove elements . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Variables and Arrays . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... String functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions for working with single characters . ... ... ... ... ... ... ... ... ... ... ... Functions for cutting off spaces . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Search in the text . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Comparison functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Formatting and outputting strings . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Composing / splitting lines . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with blocks of text . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions for converting characters . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Case change functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Setting the locale (local settings) . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Conversion of encodings . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions of format transformations . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... URL functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with binary data . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... String sums and hash functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Symbolic links. Hard links . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Date and time functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Logical functions for determining the type of a variable . ... ... ... ... ... ... ... ... ... ... Variable functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions for working with functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Calendar functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Part 3. Files and networks Working with files . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Opening a file . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Closing the file . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Reading and writing . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... The position of the current position pointer . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions for determining file types . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Defining file parameters . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions for working with file names . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions for manipulating whole files . ... ... ... ... ... ... ... ... ... ... ... ... Other functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions for working with catalogs . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Manipulating directories . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with records . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... FTP . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with an FTP server . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with files . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... IMAP functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... SNMP functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Vmailmgr functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Network functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Part 4. Control functions Tracking and processing and error . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Introduction . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Error handling functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Installing a custom error handler . ... ... ... ... ... ... ... ... Session management . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Why sessions are needed. Sessions work mechanism . ... ... ... ... ... ... ... ... ... ... ... ... Session initialization and variable registration . ... ... ... ... ... ... ... ... ... Session group name . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Session ID . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Other functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Overview of handlers . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... About sessions and cookies . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with WWW . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Setting response headers . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Getting request headers . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Ra bot of Cookies The . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... SSI and virtual () function . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Output control . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Introduction . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Output control functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... PHP script execution control . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Script control functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Connection status . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Additional functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Postal functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Functions for launching programs . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Dynamic loading functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Informational functions . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Part 5. Interaction with databases
MySQL database . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with databases . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Processing query results . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Part 6. Graphics Working with images and the GD library . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Image parameters . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Manipulating images . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with color in RGB format . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Graphic primitives . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with fixed fonts . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with TrueType and PostScript Type 1 fonts . ... ... ... ... ... ... ... ... ... ... PDF documents . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Introduction . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Opening a document . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with text . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Setting the scale and coordinate system . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Draw and fill shapes . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Placement of figures . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Document style . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Part 7. Tips on the topic Ban ke sh ation by the customer, in PHP th . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Creating a poll in PHP . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Sending emails using PHP . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... General questions, encoding issues, HTML submission File attachment . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... How to insert a picture into a letter . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... PHP to Excel: Working with COM Objects . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Introduction . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Opening, recording, closing a document . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Cell format: Alignment . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Cell format: Font . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with strings . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Working with columns . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Add / Delete / Rename Sheets . ... ... ... ... ... ... ... ... ... ... Drawing tables . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Copy / paste of cells . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 1
Part 1. Language syntax and operators Expressions if Allows you to organize the execution of code snippets conditionally. Syntax: if ( expression ) statement Can be nested indefinitely in other IFs. if ($ a> $ b) print "$ a is greater than $ b"; if ($ a> $ b) {echo "$ a is greater than $ b; $ b = $ a;} else Expands the IF capabilities in terms of handling variants of an expression when it is FALSE. The ELSE expression is executed only if the IF is FALSE. if ($ a > $ b ) { echo "a is greater than b "; } else { echo "a is less than b"; } elseif Is a combination of IF and ELSE. Allows the expression to be executed if the IF value is FALSE, but unlike ELSE it is executed if the ELSEIF expression is TRUE. if ($ a> $ b) { echo "a is greater than b"; } elseif ($ a == $ b) { echo "a is equal to b"; } else { echo "a is less than b"; } if_endif One of the possible options for grouping statements with an IF statement. Convenient for embedding large blocks of HTML code inside an IF statement. if ($ a == 1): echo "a is 1"; elseif ($ a == 2): echo "a is 2"; else: echo "not equal to 1 and 2"; endif; <? php if ($ a == 5):?> A = 5 <? php endif;?> -Block HTML code A = 5 will be visible, if the condition $ a == 5 is true while The simplest type of loop in PHP. Forces PHP to execute nested statements as long as the condition is TRUE. If the condition is FALSE from the very beginning, then the loop will not be executed once. Syntax: WHILE ( condition ) expression You can group multiple statements within curly braces, or use an alternative syntax: WHILE ( condition ) expression ... ENDWHILE;
$ a = 1; while ($ a <= 5) { echo $ a ++; } $ a = 1; while ($ a <= 5): echo $ a; $ a ++; endwhile; - These two examples print numbers from 1 to 5. do_while A loop similar to WHILE, but the value of the logical expression is checked not before, but after the end of the iteration. The main difference is that the loop will be executed at least once. $ a = 1; do { echo $ a; } while ($ a> 1); You can stop using the statement block in the middle by embedding the BREAK statement in the DO..WHILE (0) loop : do { if ($ a == 5) { ech o "A equals 5" break; } $ a * = $ b; if ($ a <$ minimum) { break; } echo "A is $ a"; } while (0); for The most powerful loop in PHP. Syntax: FOR ( condition1 ; condition2 ; condition3 ) of expression condition1 - Unconditionally executed (evaluated) at the beginning of the loop condition2 - Checked at the beginning of each iteration. If it is TRUE, then the loop continues and the nested statements are executed. If it is FALSE, then the loop ends. condition3 - Executed (evaluated) at the end of each iteration. Each of these conditions can be empty. Example 1: for ($ a = 1; $ a <= 5; $ a ++) { echo $ a; } Example 2: for ($ a = 1 ;; $ a ++) { if ($ a> 5) { break; } echo $ a; } Example 3: $ a = 1; for (;;) {
if ($ a> 5) { break; } print $ a; $ a ++; } Example 4: for ($ a = 1; $ a <= 5; print $ a, $ a ++); PHP supports an alternative syntax FOR: FOR ( conv1; conv2; conv3; ): operators; ...; ENDFOR; break Break the execution of the current loop. Example : $ a = 0; while ($ a <5) { if ($ arr [$ a] == "stop") { break; } $ a ++; } continue Goes to the beginning of the next cycle. while (list ($ key, $ value) = each ($ arr)) { if ($ key% 2) { continue; } do_something_odd ($ value); } switch Compares a variable or expression with different values and executes different pieces of code depending on what the expression's value equals to. switch ($ a) { case 0: echo "A is 0"; break; case 1: echo "A is 1"; break; case 2: echo "A is 2"; break; default: echo "A is not equal to 0, 1, 2"; } default - matches all values that do not satisfy other CASEs. CASE - can be of any scalar type, i.e. integers or floating point numbers and strings. require Replaces itself with the contents of the specified file. Example: require ("include.inc");
But you can't put it inside a loop and expect it to include the contents of another file multiple times during each iteration. There is INCLUDE for this. include Inserts and executes the contents of the specified file. $ files = array ("first.inc", "seco nd.inc", "third.inc"); for ($ a = 0; $ a <count ($ files); $ a ++) { include ($ files [$ a ]); } Because INCLUDE () is a special operator, you must enclose it in curly braces when used inside a conditional operator. if ($ a <5) { include ("file_1.in c"); } else { include ("file_2.inc"); } function A function declaration. Any valid PHP code can be inside a function, even a declaration of another function or class. Functions must be declared before they can be referenced. function foo ($ arg_1, $ arg_2, ..., $ arg_n) { echo " Sample function ."; return $ retvalue; } Returning Results: Results are returned via an optional return statement. The returned result can be of any type, including lists and objects. function my_sqrt ($ num) { retu rn $ num * $ num; } echo my _ sqrt (4); // will print 16 Multiple results cannot be returned as a result, but you can implement this by returning a list: function foo () { return array (0, 1, 2); } list ($ zero, $ one, $ two) = foo (); Arguments: Information can be passed to a function through an argument list, which is a comma-separated list of variables and / or constants. Variable length argument lists are not supported, but the same can be achieved by passing arrays. function takes_array ($ input) { echo "$ input [0] + $ input [1] =", $ input [0] + $ input [1]; } Pass by reference: By default, function arguments are passed by value. To change the arguments in a function, they must be passed by reference. To do this, put an ampersand (&) in front of the argument name in the function declaration: function foo (& $ bar ) { $ bar. = "and an extra string."; } $ str = "This is a string,"; foo ($ str); echo $ str; // will output: "This is a string, and an extra string." function foo ($ bar ) {
$ bar. = "and an extra string."; } $ str = "This is a string,"; foo ($ str); echo $ str; // will output: "This is a string," foo (& $ str); echo $ str; // prints: "This is a string, and an extra string." Default values: The default value must be a constant, not a variable or a member of a class. function day ($ type = " monday ") { echo "Today is $ type ."; } echo day (); // will output: Today is Monday. echo day ("Tuesday"); // will output: Today is Tuesday. The default arguments in the description must appear to the right of the other arguments. function day ($ day_num, $ type = " Monday ") { return " Today $ day_num is $ type."; } old_function The OLD_FUNCTION statement allows you to define a function using PHP / FI2 syntax (except that you must replace "function" with "old_function"). This property is for compatibility only and should only be used by PHP / FI2 -> PHP3 converters. Functions described in this way cannot be called from the PHP service code. You can get around this by introducing a special function in PHP3 terms that will call OLD_FUNCTION. class A set of variables and functions that operate on these variables. <? php class Cart { var $ items; // The number of items in the shopping cart // Add $ num items of type $ artnr to cart function add_item ($ artnr, $ num) { $ this-> items [$ artnr] + = $ num; } // Remove $ num $ artnr items from the cart function remove_item ($ artnr, $ num) { if ($ this-> items [$ artnr]> $ num) { $ this-> items [$ artnr] - = $ num; return true; } else { return false; } } } ?> Classes are types, that is, stubs for real variables. You must create variables of the desired type using the new operator: $ cart = new Cart; $ cart-> add_item ("10", 1); Classes can be extensions of other classes. An extended class has all the variables and functions of the base class and what you define when extending the class. This is done using the extends keyword: class Named _ Cart extends Cart { var $ owner; function set_owner ($ name) { $ t his -> owner = $ name ; }
} This defines the Named_Cart class, which has all the variables and functions of the Cart class plus an additional $ owner variable and an additional set_owner () function. You can create a named cart in the usual way and set or get the owner of the cart. You can also use the normal cart functions in a named cart: $ ncart = new Named_Cart; // Create cart $ ncart-> set_owner ( "kris" ); // Specify the owner of the print $ ncart-> owner; // Print the name of the cart owner $ ncart-> add_item ("10", 1); // inherited from regular cart 1
Part 2. Functions for working with data Math functions Rounding functions abs Returns the modulus of a number. Syntax: mixed abs (mixed $ number) The type of the $ number parameter can be float or int, and the return type is always the same as the type of this parameter. $ x = abs (-4); // $ x = 4 $ x = abs (-7.45); // $ x = 7.45 round Rounds a fraction to an integer. Syntax: double round (double $ val) Rounds $ val to the nearest integer and returns the result. $ foo = round (3.4); // $ foo == 3.0 $ foo = round (3.5); // $ foo == 4.0 $ foo = round (3.6); // $ foo == 4.0 $ x = round (5.3); // $ x = 5 $ x = round (5.4); // $ x = 5 $ x = round (5.45); // $ x = 5 $ x = round (5.5); // $ x = 6 ceil Completion of a fraction to the next integer. Syntax: int ceil (float $ number) Returns the smallest integer, at least $ number . Of course, passing an integer to $ number is pointless. $ x = ceil (5.0); // $ x = 5 $ x = ceil (5.1); // $ x = 6 $ x = ceil (5.9); // $ x = 6 floor Remove the fractional part of a number. Syntax: int floor (float $ number) Returns the maximum integer not exceeding $ number . $ x = floor (5.1); // $ x = 5 $ x = floor (5.9); // $ x = 5 Random numbers srand Initializes the random number generator. Syntax: void srand (int seed)
Initializes the random number generator to seed. srand ((double) microtime () * 1000000); $ random = rand (); echo $ random ; Here's what you get: 5645 getrandmax Returns the largest possible random number. Syntax: int getrandmax () This function returns the maximum value that can be obtained using the random number generating function rand (). Usually it is 32767 rand Generates a random number. Syntax: int rand ([int max [, int min]]) When called with the optional min and max parameters, this function generates a random number within those parameters, inclusive. If the min and max parameters are not present, a number between 0 and RAND_MAX is returned. For this function to work correctly, before using it, you need to initialize the random number generator with the srand () function. mt_rand The function returns an MT-random number, even enough even to be used in cryptography. Syntax: int mt_rand (int $ min = 0, int $ max = RAND_MAX) If you want to generate numbers not from 0 to RAND_MAX (this constant specifies the maximum allowed random number, and can be obtained by calling mt_getrandmax () ), set the appropriate interval in parameters $ min and $ max . Don't forget to just run mt_srand () before the first call to this function . mt_srand (time () + (double) microtime () * 1000000); $ x = mt_rand (1,100); // $ x is a value from 1 to 100 mt_srand Sets the MT random number generator to a new sequence. Syntax: void mt_srand (int seed) The fact is that although the numbers generated by mt_rand () are fairly equally probable, they have one drawback: the sequence of generated numbers will be the same if the script is called several times in a row. The mt_srand () function solves this problem: it selects a new sequence based on the $ seed parameter , and in an almost unpredictable way. mt_srand (time () + (double) microtime () * 1000000); for ($ i = 0; $ i <= 10; $ i ++) { $ x = mt _rand (1,10); }; In this case, the sequence is set based on the script startup time (in seconds), so it is rather unpredictable. For an even more reliable result, it is recommended to add more microseconds here (which was done), as well as the identifier of the process that called the script.
mt_getrandmax Returns the maximum MT random number. Syntax: int mt_getrandmax () Returns the maximum number that can be generated by the mt_rand () function - in other words, the RAND_MAX constant $ max = mt_getrandmax (); // $ max = 2147483647 lcg_value function generates a random fractional number. Syntax: double lcg_value () This function returns a pseudo-random fractional number in the range 0 to 1. Translation into various number systems base_convert Converting a number from one number system to another. Syntax: string base_convert (string $ number, int $ frombase, int $ tobase) Converts $ number (specified as a base string in base $ frombase ) to base $ tobase . The $ frombase and $ tobase parameters can only take values between 2 and 36, inclusive. In the string $ number, the numbers stand for themselves, and the letter a matches 11, b -12, etc. to z , which stands for 36. For example, the following commands will print 11111111 (8 ones), because this is nothing more than the binary representation of the hexadecimal number FF : $ x = base_convert ("FF", 16.2); // $ x = 11111111 $ x = base_convert ("11111111", 2.16); // $ x = FF $ x = base_convert ("200", 10 , 16); // $ x = C8 bindec Converts binary to decimal. Syntax: int bindec (string binary_string) Converts the binary number specified in binary_string to a decimal number. The maximum number that can still be converted is 2 147 483 647 $ x = bindec (11111111); // $ x = 255 $ x = bindec (10101010); // $ x = 170 $ x = bindec (2147483647); // $ x = 1111111111111111111111111111111 decbin Convert decimal to binary. Syntax: string decbin (int $ number) Returns a string representing the binary representation of the integer $ number . The maximum number that can still be converted is 2147483647, which looks like 31 ones in binary. There are similar functions for octal and hexadecimal systems. They are called the same, only instead of
"bin" are substituted respectively "oct" and "hex". $ x = decbin (255); // $ x = 11111111 $ x = decbin (2147483647); // $ x = 1111111111111111111111111111111 dechex Performs decimal to hexadecimal conversion. Syntax: string dechex (int number) Returns a string representing the hexadecimal representation of the integer number . The maximum number that can still be converted is 2,147,483,647 $ x = dechex (2147483647); // $ x = 7 fffffff decoct Performs decimal to octal conversion. Syntax: string decoct (int number) Returns a string representing the octal representation of the integer number . The maximum number that can still be converted is 2,147,483,647 $ x = dechex (2147483647); // $ x = 17777777777 hexdec Convert a hexadecimal number to decimal. Syntax: int hexdec (string hex_string) Converts the hexadecimal number specified in hex_string to a decimal number. The maximum number that can still be converted is 7fffffff $ x = hexdec (7 fffffff ); // $ x = 2147483647 octdec Convert an octal number to decimal. Syntax: int octdec (string octal_string) Converts the octal number specified in octal_string to a decimal number. The maximum number that can still be converted is 17777777777 $ x = octdec (17777777777); // $ x = 2147483647 deg2rad Converts degrees to radians. Syntax: double deg2rad (double number) Converts the degrees specified in the number parameter to radians. rad2deg Converts radians to degrees. Syntax: double rad2deg (double number) Converts the radians specified in the number parameter to degrees.
number_format Formatting a number. Syntax: number_format ($ number, $ decimals, $ dec_point = ".", $ Thousands_sep = ","); This function formats a floating point number by dividing it into triads with the specified precision. It can be called with two or four arguments, but not three! The $ decimals parameter specifies how many decimal places the number should have in the output string. The $ dec_point parameter is the integer and fractional separator, and the $ thousands_sep parameter is the triad separator in the number (if you specify an empty string in its place, the triads are not separated from each other). Minimum and maximum min This function returns the smallest of the numbers given in its arguments. Syntax: mixed min (mixed $ arg1 [int $ arg2, ..., int $ argn]) There are two ways to call this function: with one parameter or with several parameters. If only one parameter (the first) is specified, then it must be an array and the minimum element of this array is returned. Otherwise, the first (and other) arguments are treated as floating point numbers, they are compared, and the smallest is returned. The return type is chosen as follows: if at least one of the numbers passed to the input is specified in floating point format, then the result will be floating point, otherwise the result will be an integer. You cannot lexographically compare strings with this function - only numbers. $ x = min (5,3,4,6,5,6,8,9); // $ x = 3 $ x [0] = 4; $ x [1] = 1; $ x [2] = 5; $ x [3] = 2; echo min ($ x); // will output 1 max Get the largest argument. Syntax: mixed max (mixed $ arg1 [int $ arg2, ..., int $ argn]) This function works like min () , only it looks for the maximum value. $ x = max (5,3,4,6,5,6,8,9); // $ x = 9 $ x [0] = 4; $ x [1] = 1; $ x [2] = 5; $ x [3] = 2; echo max ($ x); // will print 5 Power functions sqrt Returns the square root of the argument. Syntax: float sqrt (float $ arg) If the argument is negative, a warning is generated, but the program does not terminate! $ x = sqrt (9); // $ x = 3 echo s qrt (25); // will print 5
echo sqrt (-25); // prints -1. # IND log Returns the natural logarithm of the argument. Syntax: float log (float $ arg) Prints a warning if the number is invalid, but does not terminate the program. $ x = log (exp (2)); // exp (2) - e to the power of 2 // $ x = 2 $ x = log (M_E); // $ x = 1 echo log (10); // will output 2.302585092994 log10 Returns the decimal logarithm of the argument. Syntax: float log10 (float $ arg) Prints a warning if the number is invalid, but does not terminate the program. echo log10 (100); // will output 2 exp Returns e (2.718281828) to the power of $ arg . Syntax : float exp (float $ arg) $ x = exp (1); // $ x = 2.718281828459 pow Exponentiation. Syntax: float pow (float $ base, float $ exp) Returns $ base to the power of $ exp . $ x = pow (3,2); // $ x = 9 $ x = pow ("3", 2); // $ x = 9 Trigonometry sin Returns the sine of the argument. Syntax: float sin (float $ arg) The argument is in radians. $ x = sin (M_PI_2); // $ x = 1 cos Returns the cosine of the argument. Syntax: float cos ( float $ arg ) $ x = cos (0); // $ x = 0 $ x = cos (M_PI); // $ x = -1 tan
Returns the tangent of the argument, given in radians. Syntax : float tan (float $ arg) $ x = tan (M_PI_4); // $ x = 1 acos Returns the arc cosine of the argument. Syntax: float acos ( float $ arg ) $ x = acos (0); // $ x = pi / 2 $ x = acos (1); // $ x = 0 asin Returns the inverse sine. Syntax: float asin ( float $ arg ) $ x = asin (0); // $ x = 0 $ x = asin (1); // $ x = pi / 2 atan Returns the arctangent of the argument. Syntax: flo at atan ( float $ arg ) $ x = atan (0); // $ x = 0 $ x = atan (1); // $ x = pi / 4 atan2 Get the arctangent of two numbers. Syntax: float atan2 (float $ y, float $ x) Returns the arctangent of $ y / $ x , but including the quarter that point ( $ x, $ y ) is in. This function returns the result in radians belonging to the segment from -? before ?. $ x = atan2 (1,1); // $ x = pi / 4 $ x = atan2 (-1, -1); // $ x = -3 * pi / 4 pi Returns pi - 3.14. Syntax: double pi () This function must be called with a pair of empty parentheses: $ x = pi () * 2 // $ x = 31.415926535898 Advanced Precision BCMath Functions bcadd Adds two arbitrary precision numbers. Syntax: string bcadd (string left_operand, string right_operand [, int scale]);
This function returns a string representation of the sum of the two parameters (left_operand + right_operand) with the precision specified in the optional parameter scale. The scale indicates the number of decimal places). bccomp Comparison of two arbitrary precision numbers. Syntax : int bccomp (string left_operand, string right_operand, [int scale]); Compares the number (left_operand with right_operand) and returns a result of type integer ( an integer ). The scale parameter is used to set the number of digits after the decimal point used in the comparison. If the two parts are equal, 0 is returned. If the left part is greater than the right part, +1 is returned, and if the left part is less than the right part, -1 is returned. bcdiv The division operation for two arbitrary precision numbers. Syntax : string bcdiv (string left_operand, string right_operand [, intscale]); Divides left_operand by right_operand and returns the result with the precision (decimal places) specified in the scale parameter. bcmod Returns the remainder of an integer division. Syntax: string bcmod (left_operand, string modulus); This function returns the remainder of the integer division of left_operand by modulus. bcmul Multiplication operation for two arbitrary precision numbers. Syntax: string bcmul (string left_operand, string right_operand [, int scale]); Multiplies left_operand by right_operand, and outputs the result as a string with the precision specified in the scale variable. bcpow Raises one arbitrary precision number to the power of another. Syntax: string bcpow (string x, string y, [int scale]); Raising x to the power of y. The scale parameter can be used to set the number of digits after the dot. bcscale Sets the precision of the calculation. Syntax: string bcscale (int scale); This function sets the default precision for all BCMath math functions that do not explicitly define precision. bcsqrt
Get the square root of an arbitrary precision number. Syntax: string bcsqrt (string operand [, int scale]); Returns the square root of operand. The scale parameter sets the number of digits after the decimal point in the result. bcsub Subtracts one arbitrary precision number from another. Syntax: string bcsub (string left_operand, right_operand [, int scale]); Returns the difference of the two variables specified in the parameters of the function (left_operand - right_operand) with the precision specified in the optional parameter scale. GMP functions Functions of this kind allow working with high precision integers of a certain format using the GNU MP library. This library is not included in the standard PHP package. You can download library codes and documentation from http://www.swox.com/gmp/ . The functions provided in this library can also work with regular integer arguments. In this case, they will be automatically converted to GMP format. However, to improve performance, it is still recommended to use GMP numbers. GMP functions. GMP Function Values gmp_init Creates a GMP number. Syntax: resource gmp_init (mixed number) The GMP number is generated from an integer or string argument. The string can contain a number in decimal or hexadecimal format. If it is in hexadecimal format, the number must be prefixed with 0x. $ x = gmp_init (45); $ y = gmp_init ("46"); $ z = gmp _ init ("0 xfa 4 b "); This function is optional (arguments are automatically converted to GMP format), but desirable (performance is improved by using gmp_init ()). gmp_intval Convert GMP number to integer. Syntax: int gmp_intval (resource gmpnumber) This function converts a GMP number to an integer if the received number does not exceed its maximum size. gm p_strval Convert GMP number to string. Syntax:
Comments 0
Loading comments...
Reply to Comment
Edit Comment