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 ...
随机推荐
- 使用安卓手机上的shh软件ConnectBot管理您的Linux服务器
ConnectBot是一款在Android手机上通过命令行方式连接管理类Unix系统的软件(类Unix系统包含:FreeBSD.OpenBSD.NetBSD.Solaris.Mac.AIX.GUN/L ...
- DS实验题 融合软泥怪-1
题目 思路 很容易想到第一种做法,用Huffman算法,从森林中找出两个值最小的节点,合并再加入森林,在这个过程中不断记录. 但是每一次需要sort一遍,将最小的两个值节点置于头两个节点,最坏情况下复 ...
- spring mvc@RequestParam根据参数名获取传入参数值
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...
- reset 单个文件 回退
git将单个文件恢复到历史版本的正确方法如下: git reset commit_id 文件路径 git checkout -- 文件路径
- 【java基础学习】网络编程
网络编程 InetAddress tcp udp
- ZTE交换路由设备配置的备份与恢复
一.TFTP服务器搭建 使用用户计算机搭建TFTP服务器,交换路由设备作为TFTP客户端. 运行TFTPServer.exe,该程序所在的目录为TFTP的根目录. 请保证您的TFTP可以Ping通所要 ...
- JQuery中的选择器的总结
JQuery的选择器十分的强大,但是我平常经常用到的却十分的有限,趁现在有时间我感觉有必要总结一下. 基本选择器 $("#ID") 选择ID所对应的标签元素 返回一个标签元素$(& ...
- Ubuntu 16.04 几个国内更新源
http://blog.csdn.net/paincupid/article/details/52895676 如何更改源 1/ 在修改source.list前,最好先备份一份 执行备份命令 sudo ...
- java内存泄漏的定位与分析
1.为什么会发生内存泄漏 java 如何检测内在泄漏呢?我们需要一些工具进行检测,并发现内存泄漏问题,不然很容易发生down机问题. 编写java程序最为方便的地方就是我们不需要管理内存的分配和释放, ...
- mysql 工具箱
创建测试数据 存储过程: delimiter // create procedure sp_generate_data() begin ; do set @created_time := date_a ...