PHP 笔记一(systax/variables/echo/print/Data Type)
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
<?php
// PHP code goes here
?>
2> Comments in PHP
<?php
// This is a single-line comment # This is also a single-line comment /*
This is a multiple-lines comment block
that spans over multiple
lines
*/ // You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
3> Case Sensitivity
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
However; all variable names are case-sensitive.
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
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.
<?php
$x = 5;
$y = 10; function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
} myTest();
echo $y; // outputs 15
?>
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
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
echo Display Variables
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4; echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2<br>";
echo $x + $y;
?>
print Display Text
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
print Display Variables
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4; print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
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
<?php
$x = "Hello world!";
$y = 5985;
$z = 10.365;
$cars = array("Volvo","BMW","Toyota");
// var_dump() function returns the data type and value
var_dump($cars);
8 $a = null;
Object Example
<?php
class Car {
function Car() {
$this->model = "VW";
}
} // create an object
$herbie = new Car(); // show object properties
echo $herbie->model;
?>
PHP 笔记一(systax/variables/echo/print/Data Type)的更多相关文章
- php中echo(),print(),print_r()之间的区别
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print只能打印出简单类型变量的值(如int,string) print_r可以打印出复 ...
- php学习日志(3)-echo&print
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较. echo与print的区别: echo print 连续输出字符串 能连续输出多个字符串 只能输出一个字符串 ...
- PHP中echo,print(),print_r()的区别
echo是 php 语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print() 只能打印出简单类型变量的值(如int,string) print_r ...
- php中echo(),print(),print_r()用法
原文 php中echo(),print(),print_r()用法 从我对echo(),print(),print_r()这个函数的理解是echo可输入字符串变量常量,print与echo差不多,但p ...
- echo() print() printf() print_r() 的区别
echo是一个语言结构而非函数,因此它无法被变量函数调用, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print() 只能打印出简单类型变量的值(如int ...
- PHP里echo print print_r的区别
echo ,print的区别在于echo 可以输出多个变量值,而print只有一个变量,做为一个字符串输出. 另一点区别在于echo 没有返回值,print有返回值1.print不能输出数组和对象. ...
- echo(),print(),print_r()之间的区别?
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print只能打印出简单类型变量的值(如int,string) print_r可以打印出复 ...
- PHP中echo(),print(),print_r()之间的区别?
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print只能打印出简单类型变量的值(如int,string) print_r可以打印出复杂类 ...
- Linux C double linked for any data type
/************************************************************************** * Linux C double linked ...
随机推荐
- AD Local Domain groups, Global groups and Universal groups
http://ss64.com/nt/syntax-groups.html Rules that govern when a group can be added to another group ( ...
- JAVA基础篇NO1--环境变量的配置及命名规则
标签(空格分隔): java基础 一:计算机概述 计算机:硬件和软件 硬件:控制器 运算器 存储器 输入和输出设备 存储器:外存(硬盘) 内存 软件:系统软件 应用软件 系统软件:wi ...
- source insight增加注释宏
打开base项目,下main.c文件里加入以下代码 对于在一行内的局部注释, 没有找到解除注释的宏, macro Comments_orUn_gx() { //用杠星注释多行,或取消杠星注释 ,不选中 ...
- Return Largest Numbers in Arrays
题目要求 右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组. 提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组的每个元素. 答题思路 ...
- 制作手机相册 全屏滚动插件fullpage.js
今天是端午自己做了一个小的送祝福链接 这里用到了fullpage插件 $('#container').fullpage({ navigation: false, //navigatio ...
- SQL SERVER数据库备份时出现“操作系统错误5(拒绝访问)。BACKUP DATABASE 正在异常终止。”错误的解决办法
一般备份文件选择的目录为磁盘根目录或备份所选分区未授予sqlserver用户读写权限时会出现此错误. 解决办法就是给sqlserver用户授予权限: 选择要备份的文件夹 ,右键-->属性--&g ...
- LeetCode Design Hit Counter
原题链接在这里:https://leetcode.com/problems/design-hit-counter/. 题目: Design a hit counter which counts the ...
- 一个简单的js实现倒计时函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- win7下安装mysql5.7[zip包]
原本以为很简单的安装,结果卡在一个环节,此文记录安装步奏. 1.下载mysql-5.7.16-winx64.zip 安装包 地址:http://cdn.mysql.com//Downloads/MyS ...
- Java不同编码方式,中英文字符所占字节数
测试代码 public class Test { public static void main(String[] args){ String[] charsetNames={ "UTF-8 ...