PHP&MySQL(二)-困也得啃書

WBOY
發布: 2016-07-29 09:01:01
原創
809 人瀏覽過

madan,所有事情都敢趕在一起。 。 。以後每天中午去學車啊,好開心。 。晚上好困,但是困也得啃書。 。 。 。 。 。 。 。

二、PHP腳本程式語言

什麼變數啊,資料型別啊,特別特別基本的不記錄了,說點容易忽略的。

<?<span>php
</span><span>define</span>("PI", 3.1415926);  <span>//</span><span>常量的定义</span><span>echo</span> "圆周率=".<span>PI</span>."<br/>";   <span>//</span><span>.的链接作用,同样用于$var的赋值等</span><span>if</span> (<span>PI</span><3&&<span>PI</span>>=2<span>) {  
    </span><span>$var</span>="shit"<span>;
}</span><span>else</span><span>{
    </span><span>echo</span> "f**k"."<hr/>"<span>;
}
</span><span>//</span><span> 先判断PI<3,如果不成立,就直接进入else&mdash;&mdash;称为短路设计,加快程序速度;
// 同理,为了实现程序的高效,把最容易出错的放前面,最复杂的计算放右边</span><span>$var</span>="asldkjflajdfklj"<span>;
</span><span>$v2</span>=(<span>boolean</span>)<span>$var</span>;   <span>//</span><span>强制类型转换</span><span>echo</span><span>$v2</span>;<span>echo</span> "<br/>"<span>;

</span><span>$v_true</span>=<span>true</span>;   <span>//</span><span>boolean值也可以直接赋值</span><span>if</span> (<span>is_bool</span>(<span>$v2</span>)&&<span>$v2</span>===<span>$v_true</span>) {    <span>//</span><span>恒等===,可以判断数据类型是否相同;is_bool</span><span>echo</span> "Variables have the same value and type!"<span>;  
    </span><span>echo</span> "<hr/>"<span>;
}
</span><span>//</span><span>另外,非恒等仍然是!==</span><span>$lll</span>="dfa"+333<span>;
</span><span>var_dump</span>(<span>$lll</span>);<span>echo</span> "<hr/>"<span>;
</span><span>print_r</span>(<span>$lll</span>);<span>echo</span> "<hr/>"<span>;
</span><span>echo</span><span>gettype</span>(<span>$lll</span>);<span>echo</span> "<hr/>"<span>;
</span><span>//</span><span>var_dump允许一次检查多个变量,同时提供信息;print_r多用来对数组和对象进行输出;</span><span>if</span> (<span>isset</span>(<span>$lll</span>)&&!<span>empty</span>(<span>$v2</span>)) {  <span>//</span><span>判断是否为空</span><span>unset</span>(<span>$lll</span><span>);
}
</span><span>var_dump</span>(<span>$lll</span>);<span>echo</span> "<hr/>";  <span>//</span><span>会报错,但仍能输出null</span><span>function</span> doubleint(<span>$var</span>){   <span>//</span><span>自定义函数</span><span>if</span> (<span>is_int</span>(<span>$var</span><span>)) {
        </span><span>$var</span>=<span>$var</span>*2<span>;
        </span><span>return</span><span>$var</span><span>;
    }</span><span>else</span><span>{
        </span><span>echo</span> "Please enter a int data."<span>;
    }
}

</span><span>echo</span> doubleint(<span>$var</span>);<span>echo</span> "<hr/>"<span>;
</span><span>echo</span> doubleint(4);<span>echo</span> "<hr/>"<span>;
</span><span>//</span><span>个人觉得,还是少用global和地址传递,逻辑上容易出错,程序没错,结果却不对

// 关于include和require
// 多用include吧
// include失败了只是警告,后者则是错误然后中断程序
// 同时推荐没事儿就写绝对路径,好读也不易出错</span></span>
登入後複製

要說的都在上面了,總結下就是:常數,連接符,自訂函數,類型轉換,類型判斷,檔案引用,恆等符,短路設計,var_dump。

比較亂。

最後舉個例子吧,自製乘法表

<?<span>php
</span><span>//</span><span>Chapter 2's example which is a chengfabiao</span><span>function</span> chengfabiao(<span>$var</span><span>){
    </span><span>for</span> (<span>$row</span>=1;<span>$row</span><=<span>$var</span>;<span>$row</span>++<span>){
        </span><span>for</span>(<span>$queue</span>=1;<span>$queue</span><=<span>$var</span>;<span>$queue</span>++<span>){
            </span><span>$answer</span>=<span>$row</span>*<span>$queue</span><span>;
            </span><span>echo</span> "<span>$row</span>*<span>$queue</span>=".<span>$answer</span>."  "<span>;
        }
        </span><span>echo</span> "<hr/>"<span>;
    }        
}
</span><span>$var</span>=9<span>;
chengfabiao(</span><span>$var</span>);
登入後複製


自己改著玩吧。基礎的東西也得常常拿出來溫習下,我是這麼認為的。

以上就介紹了PHP&MySQL(二)-困也得啃書,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板