New Tips for Beginners php new Road (iv)

Source: Internet
Author: User
Tags bitwise operators define function getting started with php
Getting Started with PHP
4.1 Data types
PHP supports integers, floating-point numbers, strings, arrays, and objects. The variable type is usually not determined by the programmer and is determined by the PHP run process (really good relief!). )。 Of course, if you like, you can also use cast or function settype () to convert some type of variable to the specified type.
Numerical
numeric types can be integers or floating-point numbers. You can use the following statement to assign a value to a number:
$a = 1234; # decimal Number
$a =-123; # negative
$a = 0123; # octal number (equal to 83 of decimal number)
$a = 0x12; # hexadecimal number (equal to 18 of decimal number)
$a = 1.234; # floating-point "double-precision number"
$a = 1.2e3; # The exponential form of double-precision numbers
String
A string can be defined by a field that is enclosed by single or double quotation marks. Note that the strings quoted in the enclosed quotation marks are literally defined, and the strings drawn from the double quotation marks can be expanded. Also, you can use a backslash () in a double-quote string to add escape sequences and convert characters to a string. Examples are as follows:
$first = ' Hello ';
$second = "World";
$full 1 = "$first $second"; # Generate Hello World
$full 2 = ' $first $second '; # generates $first $second
$full 3= "01DC studio,." Copyright. " " ;
Note the last line, if you need to use double quotation marks in a string, you can use a backslash character, as shown in the line statement. The backslash here is used to change the function of the double quotes.
You can connect characters and numbers with operational symbols. Characters are converted into numbers, using their original positions. There are detailed examples in the PHP manual.
Arrays and Hash tables
Arrays are supported in the same way as hash tables. How you use it depends on how you define them. You can use list () or array () to define them, or you can assign values directly to an array. The index of the array starts at 0. Although I do not explain here, but you can easily use a multidimensional array.
An array that contains two elements
$a [0] = "First";
$a [1] = "Second";
$a [] = "Third"; An easy way to add an array element
Now $A[2] is assigned the value "third"
echo count ($a); Print out 3 because the array has 3 elements
Define an array with a statement and assign a value
$myphonebook = Array (
"Sbabu" = "5348",
"Keith" = "4829",
"Carole" = "4533"
);
Oh, forget to teach long, let's add an element
$myphonebook ["Dean"] = "5397";
The Carale element you defined is wrong, let's correct it.
$myphonebook ["carole"] = "4522"
Did I not tell you how to use similar support for arrays? Let's take a look at
echo "$myphonebook [0]"; Sbabu
echo "$myphonebook [1]"; 5348
Some other functions that are useful for arrays or hash tables include sort (), Next (), Prev (), and each ().
Object
Use the new statement to produce 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 (and does not require) defining variable types directly when declaring variables, and variable types will be determined by the circumstances in which they are applied." If you assign a value to a variable var to a string, it becomes a string. If you assign an integer value 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 now an integer (2)
$foo = $foo + 1.3; $foo is a double-precision number (3.3)
$foo = 5 + "Ten Little piggies"; $foo is an integer (15)
$foo = 5 + "Ten Small Pigs"; $foo is an integer (15)
If you want to forcibly convert a variable type, you can use the same function Settype () as the C language.
4.2 Variables and constants
You may have noticed that the variable has a dollar sign ($) prefix. All variables are local variables, and the global statement is used in order to enable the use of external variables in the defined function. And you want to limit the scope of the variable to that function, using the static statement.
$g _var = 1; Global scope
function test ()
{
Global $g _var; This makes it possible to declare global variables.
}
More advanced is the variable representation of the variable. Please refer to the PHP manual. This can sometimes seem useful.
PHP has a number of defined variables built into it. You can also use the Define function to define your own constants, such as define ("CONSTANT", "value").
4.3 operator
PHP has the commonly seen operators in c,c++ and Java. The precedence of these operators is also consistent. The assignment also uses "=".
Arithmetic and character
Only one of the following operators is related to characters:
$a + $b: Plus
$a-$b: minus
$a * $b: Multiply
$a/$b: except
$a% $b: modulo (remainder)
$a. $b: String Connection
Logic and comparison
The logical operators are:
$a | | $b: Or
$a or $b: or
$a && $b: With
$a and $b: With
$a XOR $b: XOR (True if $ A or $b is true, False if both)
! $a: Non-
Comparison operators are:
$a = = $b: equal
$a! = $b: Unequal
$a < $b: less than
$a <= $b: less than or equal to
$a > $b: Greater Than
$a >= $b: greater than or equal to
As with C, PHP 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 introduce you in the following.
If, else, ElseIf, if (): endif
if (expression one)
{
. . .
}
ElseIf (Expression II)
{
. . .
}
Else
{
. . .
}
Or just like Python.
if (expression one):
. . .
. . .
ElseIf (expression two):
. . .
else:
. . .
endif;
Loops. While, do. While, for
while (expression)
{
. . .
}
Do
{
. . .
}
while (expression);
for (expression one; expression two; expression three)
{
. . .
}
Or just 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
Break interrupts the current loop control structure.
The continue is used to jump out of the remaining current loop and continue with the next loop.
Require, include
Just like the # include preprocessing in C. The file you specify in the Require will replace its location in the main file. You can use include () when you have a conditional reference file. This allows you to split a complex PHP file into multiple files and reference them separately when needed.
4.5 functions
You can define your own functions as in the example below. The return value of a function can be any data type:
function foo (variable name one, variable name two, ..., variable name N)
{
echo "Example FUNCTION.N";
return $retval;
}
All PHP code can appear in the function definition, even the definition of other functions and classes. The function must be defined before the reference.
Class 4.6
Use the class model to build the class. You can refer to the PHP manual for a detailed explanation of the class.
Class Employee
{
var $empno; Number of employees
var $empnm; Employee Name
function Add_employee ($in _num, $in _name)
{
$this->empno = $in _num;
$this-&GT;EMPNM = $in _name;
}
Function Show ()
{
echo "$this->empno, $this->empnm";
Return
}
function Changenm ($in _name)
{
$this-&GT;EMPNM = $in _name;
}
}
$sbabu = new Employee;
$sbabu->add_employee ("Sbabu");
$sbabu->changenm ("Babu");
$sbabu->show ();

The above describes the novice on the road to note the new Road (iv), including the Novice on the road to the attention of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.