Basic PHP Tutorial (i)

Source: Internet
Author: User
Tags define function echo name php tutorial alphanumeric characters

Grammar:
PHP's syntax is simple-look directly at the code: <?php/* Code section */?> This is how the PHP code is declared. Note: <,?>, etc. can also be written in this way, but it is not recommended.
The end of the marked statement: The semicolon is the end of the marked statement ";"--use ";" at the end of each statement. The semicolon indicates the end.
=====================================================================
Comments in PHP:--See Code in the tutorial
Comments in PHP have a single-line comment://This is a comment
and large module Comments:/* This is a comment */
=====================================================================
Variable:
PHP variables are loose. But it's also case-sensitive, which you should pay attention to. Before using it, there is no need to declare-depending on how the variable is declared, PHP automatically converts the variable to the correct data type.
Declaring a variable in PHP is declared using the $ keyword--all variables are identified by $
Variable naming rules:
The variable name must begin with a letter or underscore "_".
Variable names can contain only alphanumeric characters and underscores.
Variable names cannot contain spaces. If the variable name consists of multiple words, it should be delimited with an underscore (such as $my _string), or start with a capital letter (such as $myString).
Note: (Basically all programming languages have the same variable naming rules!) )

Example:

Copy CodeThe code is as follows:
<?php
declaring variables
$var _name = "Snow";
Using variables
echo $var _name;
/*
Displaying results: Snow
*/
?>


Constant:
Declaration of constants in PHP:
Declaring constants in PHP is declared using the Define function. Look directly at code.

Copy CodeThe code is as follows:
<?php
/*
The Define function has three parameters
First parameter: Specify the constant name--no keyword is used, the constant cannot have the $ symbol
Second parameter: Specify the value of the constant--only Boolean, Integer, floating point, string four types
Third parameter: Specifies whether this constant is case-sensitive--true ignores case, false is case-sensitive
*/
Define ("Name", "Zhang San", true);
echo name;
/* Show results: Zhang San--because it is true, it is not case-sensitive */
?>


There are predefined constants in PHP--you can query PHP manuals or related Materials
=====================================================================
Arrays:--php arrays are still relatively simple and useful.
The PHP array can be used as a collection in other languages
PHP arrays can contain any type of PHP support. Of course, you can also store class objects, etc.--read code directly

Copy CodeThe code is as follows:
<?php
/*===================================================================*/
Array of values
$nums = Array (+/-);
or equivalent to
$nums [0] = 1;
$nums [1] = 2;
$nums [2] = 4;
Echo $nums [2]. " <br/> ";
/* Output: 4*/
/*===================================================================*/
Associative arrays-where the "= =" is an associative symbol in PHP, is the specified key-value pair.
$ns = Array ("name" = "Zhang San", "Age" =>22, "sex" and "man");
or equivalent to
$ns ["name"] = "Zhang San";
$ns ["age"] = 22;
$ns ["sex"] = "man";
echo "Name:". $ns ["name"]. " <br/> Ages: ". $ns [" Age "]." <br/> Sex: ". $ns [" Sex "]." <br/> ";
/* Output:
Name: Zhang San
Age: 22
Gender: Man
*/
/*===================================================================*/
Multidimensional arrays--arrays can also be stored in arrays
$bs = Array ("Zhang San" =>array ("hobby" = "Computer", "age" = "23", "gender" = "male"), "Little Red" =>array ("hobby" = "eat", "sex" = "female") );
Adjust the format so that you can see clearly
$bs = array
(
"Zhang San" =>array
(
"Hobby" = "Computer",
"Age" = "23",
"Sex" = "male"
),
"Little Red" =>array
(
"Hobby" = "eat",
"Gender" and "Woman"
)
);
or equivalent to
$bs ["Little Red"] ["gender"] = 2; $bs ["Xiao Hong"] ["hobby"] = 2; //....
Or
$bs ["Zhang San"] = Array ("hobby" = "Computer", "age" = "23", "gender" = "male"); $bs ["Little red"] = Array ("hobby" = "eat", "sex" = "female");
echo $bs ["Little Red"] [gender]. " <br/> ";
/* Output: Female */
/*===================================================================*/
?>

Basic PHP Tutorial (i)

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.