phpmain
① php也有main方法嗎
php沒有談滲指main方法哦,php和c、c++等不一樣,main在c等語言是主程序入口,php並不需要,php是喊族解釋型語言。我也剛含配學php的,中山學文php培訓班。
② PHP變數的底層實現
我們解壓PHP的源碼包, 看到如下的目錄
其中,
最核心的---Zend目錄, 這是zend虛擬的實現. 包括棧,數據類型,編譯器等,都在這實現.
最主要的main --PHP的一些內建函數,最主要函數都在這里放著.
最大的一個目錄ext -- PHP的擴展.
PHP的大部分功能,都是以extenstion形式來完成的.
如果你開發了一個擴展,也放在ext目錄下.
Zend對變數的表示:
答: zend實現了 zval結構體
{
value: [聯合體] ,聯合體的內容可能是C語言中的long,double,hashtable...
type:變數類型 , IS_NULL,IS_BOOL,IS_STRING...... IS_RESOURCE
refcount_gc
is_ref_gc
}
如:
$a = 3;
{
value : [long lval = 3]
type: IS_LONG
}
$a = 3.5
{
value: [double dval = 3.5]
type:IS_DOUBLE
疑問:
PHP中有8種數據類型,為什麼zval->value 聯合體中,只有5種?
答:
1: NULL,直接 zval->type = IS_NULL,就可以表示,不必設置 value的值.
2: BOOL型 , zval->type = IS_BOOL, 再設置 zval.value.lval = 1/0;
3: Resourc型 ,資源型 往往是伺服器上打開的一個介面,如果 文件讀取介面.
zval->type = IS_RESOURCE, zval->tyoe.lval =伺服器上打開的介面的編號
發現:
PHP中,字元串類型,長度是已經緩存的,調用strlen時,系統可以直接返回其長度,不必計算.
③ PHP怎麼調用其他類的方法
在Java的調用方法是import,而在PHP中沒有import這個函數,一般PHP中調用其他類是用到require(),具體PHP調用其他類的方法如下:
1、首先應該先有一個文件名為tool.php的文件,在文件中聲明一個類。
(3)phpmain擴展閱讀:
類是變數與作用於這些變數的函數的集合。使用下面的語法定義一個類:
<?php
class Cart { var $items; // 購物車中的物品
// 將 $num 個 $artnr 物品加入購物車
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
} // 將 $num 個 $artnr 物品從購物車中取出
function remove_item($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num; return true;
} elseif
($this->items[$artnr] == $num) {
unset($this->items[$artnr]); return true;
} else {
return false; }
}
} ?>
上面的例子定義了一個 Cart 類,這個類由購物車中的商品構成的數組和兩個用於從購物車中添加和刪除商品的函數組成。
④ PHP中Fatal error: main() [<a href='function.main'>function.main</a>]: The script tried to execute a
你放入session中的應該是一個對象,而你取出這個對象之後要操作它,必須察碰鏈導入類定吵罩義的php文件才可以訪問這個對象,否則php會調用 __autoload() 來尋找該類的定義。報錯是因為找不到session中取出的類的定義,而又找不到 __autoload() 函數。
例如:
a.php
class A {
var x;
}
b.php
include a.php
$a = new A();
$a->x = '123';
session_start();
$_SESSION['a'] = $a;
c.php
include a.php //敗孫必須導入A類的定義文件a.php
session_start();
$a = $_SESSION['a'];
echo $a->x; //否者此處將報出錯誤
⑤ php如何實現腳本非同步執行的方法具體分析
php語言得用fsockopen()函數,實現腳本非同步運行,代碼如下
非同步請求函數(用debug參數若為true則為用為調試,開啟調試可以看到非同步的執行情況,但是失去非同步的效果)
main.php
<?php
/**
*非同步請求
*@rightCopyright(c)HangzhouTechnologyCo.,Ltd.(https://www.5wx.org)
*@author$Author:juny$
*@version$Id:main.php3322018-09-2309:15:08Zjuny$
*/
functionrequest_by_fsockopen($url,$post_data=array(),$debug=false){
$url_array=parse_url($url);
$hostname=$url_array['host'];
$port=isset($url_array['port'])?$url_array['port']:80;
@$requestPath=$url_array['path']."?".$url_array['query'];
$fp=fsockopen($hostname,$port,$errno,$errstr,10);
if(!$fp){
echo"$errstr($errno)";
returnfalse;
}
$method="GET";
if(!empty($post_data)){
$method="POST";
}
$header="$method$requestPathHTTP/1.1 ";
$header.="Host:$hostname ";
if(!empty($post_data)){
$_post=strval(NULL);
foreach($post_dataas$k=>$v){
$_post[]=$k."=".urlencode($v);//必須做url轉碼以防模擬post提交的數據中有&符而導致post參數鍵值對紊亂
}
$_post=implode('&',$_post);
$header.="Content-Type:application/x-www-form-urlencoded ";//POST數據
$header.="Content-Length:".strlen($_post)." ";//POST數據的長度
$header.="Connection:Close ";//長連接關閉
$header.=$_post;//傳遞POST數據
}else{
$header.="Connection:Close ";//長連接關閉
}
fwrite($fp,$header);
//-----------------調試代碼區間-----------------
//注如果開啟下面的注釋,非同步將不生效可是方便調試
if($debug){
$html='';
while(!feof($fp)){
$html.=fgets($fp);
}
echo$html;
}
//-----------------調試代碼區間-----------------
fclose($fp);
}
$data=array('name'=>'guoyu','pwd'=>'123456');
$url='http://localhost/test/other.php';
request_by_fsockopen($url,$data,true);//
other.php
<?php
header("content-type:text/html;charset=utf-8");
//error_reporting(0);
//ini_set('html_errors',false);
//ini_set('display_errors',false);
$name=isset($_POST['name'])?$_POST['name']:'';
$pwd=isset($_POST['pwd'])?$_POST['pwd']:'';
echo$name.$pwd;
echo'successok';
die;
?>
使用實例:
[運行的main.php主腳本文件]$data=array('name'=>'guoyu','pwd'=>'123456');
$url='http://localhost/test/other.php';
request_by_fsockopen($url,$data,true);//把應用B的用戶表非同步-同步數據
[導步執行文件other.php]
在other.php中便可以用$_POST接收main.php提交過來的參數,從而進行下一步操作
以上就是php如何實現腳本非同步執行的方法具體分析的詳細內容.
⑥ php 怎麼截取二級域名
如何截取一個URL中的主域名。
<?php
$S = 'http://www1.unue.cn';
$S = parse_url($S);
$S = strtolower($S['host']) ; //取域名部分
//echo $S;
$domain = array('com','cn','name','org','net'); //域名後綴 有新的就擴展這吧
$SS = $S;
$dd = implode('|',$domain);
$SS = preg_replace('/(\.('.$dd.'))*\.('.$dd.')$/iU','',$SS); 把後面的域名後綴部分去掉
$SS = explode('.',$SS);
$SS = array_pop($SS); //取最後的主域名
$SS = substr($S,strrpos($S,$SS)); //加上後綴拼成完成的主域名
echo $SS;
?>
代碼
<?
function PMA_getenv($var_name) {
if (isset($_SERVER[$var_name])) {
return $_SERVER[$var_name];
} elseif (isset($_ENV[$var_name])) {
return $_ENV[$var_name];
} elseif (getenv($var_name)) {
return getenv($var_name);
} elseif (function_exists('apache_getenv')
&& apache_getenv($var_name, true)) {
return apache_getenv($var_name, true);
}
return '';
}
if (empty($HTTP_HOST)) {
if (PMA_getenv('HTTP_HOST')) {
$HTTP_HOST = PMA_getenv('HTTP_HOST');
} else {
$HTTP_HOST = '';
}
}
echo htmlspecialchars($HTTP_HOST);
?>
⑦ 一個網站所有php文件 打開後都指向main.php 不知道為什麼 我都無語了
一般使用了MVC結構的,都會把 調用文件放在一鎮畝個php中,如:main.php; 為控製程序文件;
再由main.php按照不同的獲取去調用不同的處理函銷段數或方法!
所以這樣設計是正確的;也很合御斗森理;
如果把所有的代碼都寫在同一支程序文件里去處理功能,這樣的設計反而是不主張的!
⑧ 怎麼用PHP創建目錄和子目錄
<?php
header("Content-type:text/html;charset=utf-8");
//設置要創建的目錄(可設置多級)
$path="/";
//首先判斷目錄存在否
if(is_dir($path)){
echo"抱歉,目錄".$path."已存在!";
}else{
//第3個參數「true」意思是能創建多級目錄,iconv防止中文目錄亂碼
$res=mkdir(iconv("UTF-8","GBK",$path),0777,true);
if($res){
echo"$path創建成功";
}else{
echo"$path創建失敗";
}
}
?>