Chubby Learn PHP summary 2-php b variable and assignment

Source: Internet
Author: User
Tags echo message learn php
I. GENERAL statement

Although PHP is a weakly typed language, it is sometimes necessary to use a type conversion. Here's a look at type conversions and defining variables and assigning values to variables.

1.1 Type conversions

The type conversion in PHP is as simple as the C language, with only the type name enclosed in parentheses before the variable.

 
  '; Echo ' output variable $num value: '. $num, Echo '

' Echo ' uses the Settype function to convert the variable $num type result: '. Settype ($num, ' Integer '); Echo '

'; Echo ' output variable $num value: '. $num, Echo '

';? >

The Settype () function in which the specified variable can be converted to the specified data type.

There are some functions in PHP that can be used to detect whether a variable is a specified type, such as Is_bool () is to detect whether it is a Boolean type, is_string () to detect whether it is a string type, and so on.

1.2 Defining constants

Constants can be understood as values unchanged, constant values are defined, can not be changed anywhere else in the script, the syntax is: define (constant_name,value,case_sensitive), three parameters are constant name (required), the value of the constant (required), is case sensitive (optional). There are two ways to get a constant: the first is to use the variable name directly, and the second is to get it through the constant () function. To determine if a constant is already defined, you can use the defined (Stringname) function to return true successfully, otherwise false.

 
  ';d efine (' count1 ', ' constant value 2 '); echo count1; $name = ' count1 '; echo constant ($name). '
'; Actually the output of the count of this constant is echo defined (' Message '). '
';? >
 
  '; Echo ' Current number of rows: '. __line__. '
'; 82echo ' Current PHP version information: '. Php_version. '
'; Echo ' current operating system: '. Php_os;echo '

';? >

1.3 Defining variables and assigning values to variables

Unlike many languages, it is not necessary to declare a variable before using it in PHP (PHP4.0 needs to declare a variable before), just assign a value to the variable.

 
  The value of the '; Echo ' variable string1 is: '. $string 1. '
'; Echo '

';//reference Assignment $i = ' SPCN '; $j = & $i; $i = "Hello, $i"; The value of Echo ' J is: '. $j. '
The value of Echo ' I is: '. $i. '
';? >

 
  ';    Global $zyy;    echo $zyy. '
';} LXT ();? > '; echo $ $change _name; The implementation principle is similar to the escape character, $change _name represents trans, and then adds a $ symbol, that is, the output is $trans echo '

';? >


1.4 operator

Operators, like other languages, include +? * / () | ^ ~ <<>< p=""> <>

 
  ';? >
 
   '; Echo '

';? >

1.5 functions

function is to write some of the reused functionality in a separate code fast, call it separately when needed, create the function syntax: Functions fun_name ($str 1, $str 2 ... $strn) {}, and then call Fun_name (XXX).

 
  ';} Echo CountNumber (10,10); Echo '

';///function pass parameter//By value Mode function Example ($m) { $m = $m * 5 +; Echo ' $m within the function is: '. $m. '
';} $MM = 1;example ($MM); The value of $m outside the Echo ' function is: '. $mm. '
'; Echo '

';//by reference mode function example1 (& $m) { $m = $m * 5 +; Echo ' $mmm within the function is: '. $m. '
';} $mmm = 1;example1 ($mmm); The value of $mmm outside the Echo ' function is: '. $mmm. '
'; Echo '

';///optional parameter, wherein $tax is optional parameter, fill also line, do not fill the function values ($price, $tax = "") { $price + = $tax; Echo ' Price is: '. $price. '
';} VALUES (100,20), values (+), Echo '

';//refer to function &rexample2 ($tmp =0) { $tmp = $tmp. ' 123456 '; return $tmp; } $str 5 = &example2 ("Kankan"); Echo $str 5. '

';? >


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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