PHP beginners (4)

Source: Internet
Author: User
Tags define function
PHP entry 4.1 data type PHP supports integers, floating-point numbers, strings, arrays, and objects. The variable type is usually determined by the PHP running process rather than the programmer (it is a good relief !). Of course, if you like, you can also use cast or settype () to convert a type of variable to a specified type. The value type can be integer or floating point.PHP entry

4.1 data type

PHP supports integers, floating point numbers, strings, arrays, and objects. The variable type is usually determined by the PHP running process rather than the programmer (it is a good relief !). Of course, if you like, you can also use cast or settype () to convert a type of variable to a specified type.

Value

The value type can be an integer or a floating point number. You can use the following statement to assign a value to a value:
$ A = 1234; # decimal number
$ A =-123; # negative number
$ A = 0123; # octal number (equal to 83 in decimal format)
$ A = 0x12; # hexadecimal number (18 equal to the decimal number)
$ A = 1.234; # floating point number "double precision"
$ A = 1.2e3; # exponential form of double precision

String

A string can be defined by single or double quotation marks. Note that the strings enclosed by single quotes are literally defined, while strings enclosed by double quotes can be extended. In addition, you can use a backslash () to add escape sequences and conversion characters to double quotation marks. Example:

$ First = 'hello ';
$ Second = "World ";
$ Full1 = "$ first $ second"; # generate Hello World
$ Full2 = '$ first $ second'; # Generate $ first $ second
$ Full3 = "01DC studio,." 2000 copyright ."";

Pay attention to the last line. if you need to use double quotation marks in the string, you can use the backslash character, as shown in the statement of this line. The backslash here is used to change the function of double quotation marks.

You can connect characters and numbers with operator numbers. The character is converted to a number to use its original position. Detailed examples are provided in the PHP Manual.

Array and hash table

Arrays and hash tables are supported in the same way. How to use it depends on how you define them. You can use list () or array () to define them, or assign values to the array directly. The index of the array starts from 0. Although I have not explained it here, you can easily use multi-dimensional arrays.

// An array containing two elements
$ A [0] = "first ";
$ A [1] = "second ";
$ A [] = "third"; // simple method for adding array elements
// Now $ a [2] is assigned "third"
Echo count ($ a); // Print 3 because the array has three elements
// Define an array with a statement and assign values
$ Myphonebook = array (
"Sbabu" => "5348 ",
"Keith" => "4829 ",
"Carole" = & gt; "4533"
);
// Oh, forget the instructor. let's add an element.
$ Myphonebook ["dean"] = "5397 ";
// The carale element you defined is incorrect. let's correct it.
$ Myphonebook ["carole"] => "4522"
// I have not told you how to use the similar support method of array? Let's take a look
Echo "$ myphonebook [0]"; // sbabu
Echo "$ myphonebook [1]"; // 5348

Other functions useful to arrays or hash tables include sort (), next (), prev (), and each ().

Object

Use the new statement to generate an object:
Class foo
{
Function do_foo ()
{
Echo "Doing foo .";
}
}
$ Bar = new foo;
$ Bar-> do_foo ();

Change variable type

As mentioned in the PHP Manual, "PHP does not support (or is not required) the variable type is defined when a variable is declared. the variable type is determined by the application. If you assign a value to the variable var as a string, it becomes a string. If you assign an integer to it, it becomes an integer. "

$ Foo = "0"; // $ foo is a string (ASCII 48)
$ Foo ++; // $ foo is the string "1" (ASCII 49)
$ Foo + = 1; // $ foo is an integer (2)
$ Foo = $ foo + 1.3; // $ foo is a double precision (3.3)
$ Foo = 5 + "10 Little Piggies"; // $ foo is an integer (15)
$ Foo = 5 + "10 Small Pigs"; // $ foo is an integer (15)

If you want to forcibly convert the variable type, you can use the same function settype () as the C language ().

4.2 variables and constants

You may have noticed that all variables have a dollar sign ($) prefix. All variables are local variables. to enable the defined function to use external variables, use the global statement. You must limit the scope of the variable to the function and use the static statement.
$ G_var = 1; // global range
Function test ()
{
Global $ g_var; // you can declare global variables.
}

More advanced is variable representation. See the PHP Manual. This is sometimes useful.

PHP has many built-in defined variables. You can also use the define function to define your own constants, such as define ("CONSTANT", "value ").

4.3 operator

PHP has common operators in C, C ++, and Java. These operators have the same priority. "=" Is also used for value assignment ".

Arithmetic and character

Only one of the following operators is related to characters:
$ A + $ B: add
$ A-$ B: minus
$ A * $ B: multiplication
$ A/$ B:
$ A % $ B: modulus (remainder)
$ A. $ B: string connection

Logic and comparison

Logical operators include:
$ A | $ B: or
$ A or $ B: or
$ A & $ B: and
$ A and $ B:
$ A xor $ B: exclusive or (true if $ a or $ B is true, false if both are the same)
! $ A: non
Comparison operators:
$ A = $ B: Equal
$! = $ B: Unequal
$ A <$ B: less
$ A <= $ B: less than or equal
$ A> $ B: greater
$ A >=$ B: greater than or equal
Like C, PHP also has a triple operator (? :). Bitwise operators also exist in PHP.

Priority

Just like C and Java!

4.4 Control Process Structure

PHP has the same process control as C. I will give a rough introduction below.

If, else, elseif, if (): endif

If (expression 1)
{
...
}
Elseif (expression 2)
{
...
}
Else
{
...
}
// Or like Python
If (expression 1 ):
...
...
Elseif (expression 2 ):
...
Else:
...
Endif;

Loops. while, do... while,

While (expression)
{
...
}
Do
{
...
}
While (expression );
For (expression 1; expression 2; Expression 3)
{
...
}
// Or like Python
While (expr ):
...
Endwhile;

Switch

Switch is the best replacement for multiple if-elseif-else structures:
Switch ($ I)
{
Case 0:
Print "I equals 0 ";
Case 1:
Print "I equals 1 ";
Case 2:
Print "I equals 2 ";
}

Break, continue

The break interrupts the current loop control structure.
Continue is used to jump out of the remaining current loop and continue to execute the next loop.

Require, include

Just like # include preprocessing in C. The file you specified in require will replace its location in the main file. You can use include () when referencing a file with conditions (). This allows you to split complex php files into multiple files and reference them separately when needed.

4.5 functions

You can define your own functions as in the following example. The return value of a function can be any data type:
Function foo (variable name 1, variable name 2,..., variable name n)
{
Echo "Example function. n ";
Return $ retval;
}

All PHP code can appear in function definitions, and even include definitions of other functions and classes. The function must be defined before reference.

4.6 Categories

Create a class using the class model. For more information about classes, see The PHP Manual.
Class Employee
{
Var $ empno; // Number of employees
Var $ empnm; // employee name

Function add_employee ($ in_num, $ in_name)
{
$ This-> empno = $ in_num;
$ This-> empnm = $ in_name;
}

Function show ()
{
Echo "$ this-> empno, $ this-> empnm ";
Return;
}

Function changenm ($ in_name)
{
$ This-> empnm = $ in_name;
}
}

$ Sbabu = new Employee;
$ Sbabu-> add_employee (10, "sbabu ");
$ Sbabu-> changenm ("babu ");
$ Sbabu-> show ();


Type = "text/javascript"> Script type = "text/javascript" src = "http://pagead2.googlesyndication.com/pagead/show_ads.js? 7.1.2 "> script(Responsible editor: Beyond PHP)
[Recommended to friends] [display printed version] Updated on: 2002-08-24 views: language = 'javascript 'src = '/view. php? Aid = 47 '> script
Language = "JavaScript" src = "/note_show.php" type = "text/javascript"> script
View source | feedback | send page | sitemap | aboutus
Copyright©2002-2006 phpe team. All rights reserved
Last updated: 04/21/06 04:55:56 China Standard Time
Server sorted soredCncms.org

Src = "http://www.google-analytics.com/urchin.js? 7.1.2 "type =" text/javascript "> script type =" text/javascript "> _ uacct =" UA-156767-2 "; urchinTracker (); script language = "JavaScript" type = "text/javascript" src = "/js/pphlogger language =" JavaScript "type =" text/javascript "src ="/site_online.php? Hide = 1 & file = article_show.php "> script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.