码迷,mamicode.com
首页 > Web开发 > 详细

PHP 笔记一(systax/variables/echo/print/Data Type)

时间:2016-01-07 00:58:59      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

PHP stands for "Hypertext Preprocessor" ,it is a server scripting language.

What Can PHP Do?

  • PHP can generate dynamic page content
  • PHP can create, open, read, write, delete, and close files on the server
  • PHP can collect form data
  • PHP can send and receive cookies
  • PHP can add, delete, modify data in your database
  • PHP can be used to control user-access
  • PHP can encrypt data

1> Basic PHP Syntax

1 <?php
2 // PHP code goes here
3 ?>

2> Comments in PHP

 1 <?php
 2 // This is a single-line comment
 3 
 4 # This is also a single-line comment
 5 
 6 /*
 7 This is a multiple-lines comment block
 8 that spans over multiple
 9 lines
10 */
11 
12 // You can also use comments to leave out parts of a code line
13 $x = 5 /* + 15 */ + 5;
14 echo $x;
15 ?>

3> Case Sensitivity

In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.

1 <?php
2 ECHO "Hello World!<br>";
3 echo "Hello World!<br>";
4 EcHo "Hello World!<br>";
5 ?>

However; all variable names are case-sensitive.

1 <?php
2 $color = "red";
3 echo "My car is " . $color . "<br>";
4 echo "My house is " . $COLOR . "<br>";
5 echo "My boat is " . $coLOR . "<br>";
6 ?>

4> PHP Variables

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)

PHP is a Loosely Typed Language

PHP Variables Scope

PHP has three different variable scopes:

  • local
  • global
  • static

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

 1 <?php
 2 $x = 5;
 3 $y = 10;
 4 
 5 function myTest() {
 6     $GLOBALS[‘y‘] = $GLOBALS[‘x‘] + $GLOBALS[‘y‘];
 7 }
 8 
 9 myTest();
10 echo $y; // outputs 15
11 ?> 

5> PHP echo and print Statements

echo and print are more or less the same. They are both used to output data to the screen.

The differences are small:

  • echo has no return value while print has a return value of 1 so it can be used in expressions.
  • echo can take multiple parameters (although such usage is rare) while print can take one argument.
  • echo is marginally faster than print.

echo Display Text

1 <?php
2 echo "<h2>PHP is Fun!</h2>";
3 echo "Hello world!<br>";
4 echo "I‘m about to learn PHP!<br>";
5 echo "This ", "string ", "was ", "made ", "with multiple parameters.";
6 ?> 

echo Display Variables

 1 <?php
 2 $txt1 = "Learn PHP";
 3 $txt2 = "W3Schools.com";
 4 $x = 5;
 5 $y = 4;
 6 
 7 echo "<h2>$txt1</h2>";
 8 echo "Study PHP at $txt2<br>";
 9 echo $x + $y;
10 ?> 

print Display Text

1 <?php
2 print "<h2>PHP is Fun!</h2>";
3 print "Hello world!<br>";
4 print "I‘m about to learn PHP!";
5 ?>

print Display Variables

 1 <?php
 2 $txt1 = "Learn PHP";
 3 $txt2 = "W3Schools.com";
 4 $x = 5;
 5 $y = 4;
 6 
 7 print "<h2>$txt1</h2>";
 8 print "Study PHP at $txt2<br>";
 9 print $x + $y;
10 ?> 

6> PHP Data Types

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

Example

1 <?php 
2 $x = "Hello world!";
3 $y = 5985;
4 $z = 10.365;
5 $cars = array("Volvo","BMW","Toyota");
6 // var_dump() function returns the data type and value
7 var_dump($cars);
8 $a = null;

Object Example

 1 <?php
 2 class Car {
 3     function Car() {
 4         $this->model = "VW";
 5     }
 6 }
 7 
 8 // create an object
 9 $herbie = new Car();
10 
11 // show object properties
12 echo $herbie->model;
13 ?> 

 

PHP 笔记一(systax/variables/echo/print/Data Type)

标签:

原文地址:http://www.cnblogs.com/hzj680539/p/5107848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!