Learn PHP quickly from C language

Source: Internet
Author: User
Tags api manual function definition learn php

PHP is an explanatory language and a common language in web development. For web programming, it is recommended that you refer to W3cschool's online API manual when learning.

PHP and C language and similar, people who understand C can write a simple PHP program with a little learning. Here are some of the differences between PHP and C language syntax:

    • 1. Constants

Define a constant in PHP with the Define () function. A valid constant name can only begin with a letter and an underscore, followed by any letter, number, or underscore. Once a constant is defined, it cannot be modified or de-defined.

such as: Define ("PI", 3.14); Defines a constant, $area = Pi*r*r; Calculates the area of a circle.

Define cannot define constants in a class, the class needs to use the const constant = ' constant value '; Define constants.

    • 2. Variables

PHP uses the dollar symbol ($) followed by the variable name to represent a variable, such as $var is a variable. PHP is a weakly typed language that determines the data type of a variable according to the context of the program.      That is, when defining a variable, you do not need to explicitly specify the type of the variable, assigning an integer to the variable $v, then $v is an integer variable. To convert a type, you can add the target type to the variable you want to convert, like the C language, and the forced type conversions allowed in PHP are: (int), (integer)--Convert to Integer. (bool), (Boolean)--converts to bool type, (float), (double), (real)--converts to floating point type, (String)--converts to a string, (array)--Converts an array, (object)--converts to an object.

PHP supports eight primitive types (type).

Four types of scalar:

String (String)

Integer (integer type)

Float (floating point type, also double)

Boolean (Boolean)

Two kinds of composite types:

Array (arrays)

Object (Objects)

Two special types:

Resource (resources)

Null (empty)

In PHP, you can use the following function to judge the type of the variable, such as the function Is_integer determine whether the variable is an integer, the other similar functions have is_string,is_double,is_array. You can also use a predefined function gettype to get the type of a variable, using a predefined function Settype to set the type of a variable.

Alternatively, you can use the predefined function isset to determine if a variable is already defined, and use the unset statement to delete a variable.

    Local variables are valid inside the existing function, the scope of the global variable is the scope of the file (limited to a single PHP file), if the local variables with the same name of the global variable, the same as the C processing mode, the global is masked. A little bit of a hassle is that if you want to access global variables within a function, you need to use the Global keyword declaration, otherwise PHP will be considered a local variable with the same name as the global variable.

PHP provides a number of predefined variables that can be used anywhere in a program or file, such as $globals, $SERVER, $_get, $_post, $_file,$_cookie.

mutable variables are a very special concept in PHP, which refers to a variable that takes the value of a variable as its own variable name, such as:$a=' Hello ';$$a=' World ';Echo"${$a}"The output is world.

Ps:php is also case-sensitive, PHP has references, but no pointers, but there is a pointer-like object type.

    • 3. Arrays

    The array elements of PHP can be dynamically grown, which is not in C. Adding an element to an array is simple, just as you would access the element, except that the key name is new or empty, such as $arr [new]=3; or $arr []=3, the result is that the PHP array $arr add an element with a key named new whose value is 3, and if the key name is empty, then the maximum value of the current integer key name plus 1 is assigned to the new element as the default key name.

    • 4. Expressions and operators

As in the C language. PHP has more "= = =" means congruent, "." Represents a string connection. "XOR" represents a logical XOR.

    • 5. Statements

PHP and C language, with the IF, else, ElseIf, switch, while, Do-while, for, break, continue, Goto and other control statements. Note that PHP is written as ElseIf, while the C language is Chinese else if, and PHP introduces a foreach control statement.

    • function definition and invocation

     functionRecursion($a)
{
if ($a<20) {
Echo"$a\ n ";
Recursion($a+1);
}
}

Compared with the C language, more function declaration, if you need to return a value, directly inside the function body return value can be.

    • PHP delimiter

Because PHP is an embedded scripting language, you need to use some kind of delimiter to distinguish between PHP code and HTML content, here is the delimiter is "<?php" and "?>", they will be included in the PHP code, that is, all the PHP code should be written in "< PHP "and"?> ".

    • Program notes

As in C, just add the # number as a single-line comment, the original//single-line comment is still valid.

    • Definition and use of classes
1 classEmployee2 { 3     Private $name; 4     Private $title; 5     protected $wage; 6 7     protected functionClockin () {8       Echo"Member$this->name clocked in ".Date("H:i:s"); 9     } Ten  One     protected functionclockout () { A       Echo"Member$this->name clocked out at ".Date("H:i:s");  -     }  -}

Classes are instantiated and used:

1 Object New  23object,4Object

If you want to access the properties or methods of a member within a defined class, you can use a pseudo-variable $this. $this used to represent the current object or the object itself.

    

Learn PHP quickly from C language

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.