PHP学习笔记(一)

<span style="font-family: 'Times New Roman'; font-size: 16px;"></span><div style="font-family: Arial; margin-top: 1px; margin-right: 4px; margin-bottom: 2px; margin-left: 2px; background-color: rgb(255, 255, 255); line-height: 1.6; font-size: 14px;">

<h2 style="font-weight: bold; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">学习和使用JAVA已经超过5年的时间了,现在准备以JAVA为基础学习PHP语言,主要从PHP和JAVA的区别方面入手进行学习</h2>
<div><br>
<div>
<span style="font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(249, 249, 249);"></span><h2 style="font-weight: bold; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">什么是 PHP?</h2>
<ul style="margin-top: 10px; margin-bottom: 0px;">
<li style="margin-top: 3px;">PHP 指 PHP:超文本预处理器(译者注:PHP: Hypertext Preprocessor,递归命名)</li>
<li style="margin-top: 3px;">PHP 是一种服务器端的脚本语言,类似 ASP</li>
<li style="margin-top: 3px;">PHP 脚本在服务器上执行</li>
<li style="margin-top: 3px;">PHP 支持很多数据库(MySQL、Informix、Oracle、Sybase、Solid、PostgreSQL、Generic ODBC 等等)</li>
<li style="margin-top: 3px;">PHP 是一个开源的软件(open source software,OSS)</li>
<li style="margin-top: 3px;">PHP 可免费下载使用</li>
</ul>
<span style="font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(249, 249, 249);"></span><h2 style="font-weight: bold; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">什么是 PHP 文件?</h2>
<ul style="margin-top: 10px; margin-bottom: 0px;">
<li style="margin-top: 3px;">PHP 文件可包含文本、HTML 标签以及脚本</li>
<li style="margin-top: 3px;">PHP 文件向浏览器返回纯粹的 HTML</li>
<li style="margin-top: 3px;">PHP 文件的文件后缀是 ".php"、".php3" 或 ".phtml"</li>
</ul>
<br>

<div><br>
<h2 style="font-weight: bold; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">PHP和JAVA的区别</h2>
<div>
<span style="font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(249, 249, 249);"></span><h2 style="font-weight: bold; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">区别一:基本的 PHP 语法</h2>
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">PHP 的脚本块以[b]<?php[/b]开始,以[b]?>[/b]结束。您可以把 PHP 的脚本块放置在文档中的任何位置。

<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">不过,为了达到最好的兼容性,我们推荐您使用标准形式 (<?php),而不是简写形式。
<?php

?></pre>
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">PHP 文件通常会包含 HTML 标签,就像一个 HTML 文件,以及一些 PHP 脚本代码。

<pre style="width: 580px; margin-top: 10px; margin-bottom: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-color: initial; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: rgb(119, 136, 85); border-right-color: rgb(119, 136, 85); border-bottom-color: rgb(119, 136, 85); border-left-color: rgb(119, 136, 85); background-color: rgb(245, 245, 245); font-family: 'Courier New', monospace; font-size: 12px;"><html>
<body>

<?php
echo "Hello World";
?>

</body>
</html></pre>
<br>

<div><br>
<h2 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">区别二:PHP 中的变量</h2>
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;"><span style="font-weight: normal;">变量用于存储值,比如数字、文本字符串或数组。</span>

<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;"><span style="font-weight: normal;">PHP 中的所有变量都是以 $ 符号开始的。</span>

<pre style="width: 580px; margin-top: 10px; margin-bottom: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-color: initial; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: rgb(119, 136, 85); border-right-color: rgb(119, 136, 85); border-bottom-color: rgb(119, 136, 85); border-left-color: rgb(119, 136, 85); background-color: rgb(245, 245, 245); font-family: 'Courier New', monospace; font-size: 12px;"><span style="font-weight: normal;">$var_name = value;</span></pre>
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;"><span style="font-weight: normal;">PHP 的入门者往往会忘记在变量的前面的 $ 符号。如果那样做的话,变量将是无效的。</span>
<h2 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;"><br></h2>区别三:PHP 是一门松散类型的语言(Loosely Typed Language)
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">在上面的例子中,您看到了,不必向 PHP 声明该变量的数据类型。

<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">在强类型的编程语言中,您必须在使用前声明变量的类型和名称。

<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;"><br>

<h2 style="font-weight: bold; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">foreach 语句</h2>


<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">foreach 语句用于循环遍历数组。

<h3 style="font-weight: bold; margin-top: 20px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px;">语法</h3>
<pre style="width: 580px; margin-top: 10px; margin-bottom: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-color: initial; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: rgb(119, 136, 85); border-right-color: rgb(119, 136, 85); border-bottom-color: rgb(119, 136, 85); border-left-color: rgb(119, 136, 85); background-color: rgb(245, 245, 245); font-family: 'Courier New', monospace; font-size: 12px;">foreach (array as value)
{
code to be executed;
}
</pre>
<h3 style="font-weight: bold; margin-top: 20px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px;">例子</h3>
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">下面的例子示范了一个循环,这个循环可以输出给定数组的值:
<html>
<body>

<?php
$arr=array("one", "two", "three");

foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>

</body>
</html></pre>
<div style="font-family: Arial; margin-top: 1px; margin-right: 4px; margin-bottom: 2px; margin-left: 2px; background-color: rgb(255, 255, 255); line-height: 1.6; font-size: 14px;"><br>
<div style="font-family: Arial; margin-top: 1px; margin-right: 4px; margin-bottom: 2px; margin-left: 2px; background-color: rgb(255, 255, 255); line-height: 1.6; font-size: 14px;"><br>
<span style="font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(249, 249, 249);"></span><h2 style="font-weight: bold; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">创建 PHP 函数</h2>
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">函数是一种可以在任何被需要的时候执行的代码块。
<h3 style="font-weight: bold; margin-top: 20px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px;">创建 PHP 函数:</h3>
[list=1]
<li style="margin-top: 3px;">所有的函数都使用关键词 "function()" 来开始</li>
<li style="margin-top: 3px;">命名函数 - 函数的名称应该提示出它的功能。函数名称以字母或下划线开头。</li>
<li style="margin-top: 3px;">添加 "{" - 开口的花括号之后的部分是函数的代码。</li>
<li style="margin-top: 3px;">插入函数代码</li>
<li style="margin-top: 3px;">添加一个 "}" - 函数通过关闭花括号来结束。</li>
[/list]
<h3 style="font-weight: bold; margin-top: 20px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px;">例子</h3>

<pre style="width: 580px; margin-top: 10px; margin-bottom: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-color: initial; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: rgb(119, 136, 85); border-right-color: rgb(119, 136, 85); border-bottom-color: rgb(119, 136, 85); border-left-color: rgb(119, 136, 85); background-color: rgb(245, 245, 245); font-family: 'Courier New', monospace; font-size: 12px;"><html>
<body>

<?php
function writeMyName()
{
echo "David Yang";
}

writeMyName();
?>

</body>
</html></pre>
<br><span style="font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(249, 249, 249);"></span>

<h2 style="font-weight: bold; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px;">PHP 函数 - 返回值</h2>
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">函数也能用于返回值。
<h3 style="font-weight: bold; margin-top: 20px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px;">例子</h3>
<html>
<body>

<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}

echo "1 + 16 = " . add(1,16);
?>

</body>
</html></pre>
<p style="margin-top: 12px; margin-bottom: 0px; line-height: 18px;">以上代码的输出:
<pre style="width: 580px; margin-top: 10px; margin-bottom: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-color: initial; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: rgb(119, 136, 85); border-right-color: rgb(119, 136, 85); border-bottom-color: rgb(119, 136, 85); border-left-color: rgb(119, 136, 85); background-color: rgb(245, 245, 245); font-family: 'Courier New', monospace; font-size: 12px;">1 + 16 = 17</pre>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值