① php多態代碼實例
這篇文章主要介紹了PHP多態代碼實例,本文用2個代碼實例來演示PHP中的多態,需要的朋友可以參考下
多態定義:只關心一個介面或者基類,而不關心一個對象的具體類。(同一類型,不同結果)
這里兩個例子:
第一個,我們發現,基類定義了標准,子類進行了自我規則的實現。這是多態的一個要求。同時,這是滿足重寫;實際上這是不同類的不同表現;沒有嚴格滿足一個介面,或者基類編程。因為你調用的時候不是
stu-showGrade()
而是各自自己的方法;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class
stu{
public
function
showGrade(){
echo
base
class;
}
}
class
xiaomin
extends
stu{
public
function
showGrade(){
echo
is
son
show
80;
}
}
class
xiaoli
extends
stu{
public
function
showGrade(){
echo
is
son
show
60;
}
}
function
doit($obj){
if(get_class($obj)
!=
stu){
$obj-showGrade();
}
}
doit(new
xiaoli());
doit(new
xiaomin());
第二個例子:dovoice
參數規定了$obj
為animal,意識就是用介面
接受了
實現類對象。了向上轉型。這就符合同一類型,不同結果了,這就是多態;
實際上在Java中
會是
animal
a
=
new
dog();這樣子的;因為PHP
是若類型語言。沒有對象轉型機制。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
interface
animal{
public
function
voice();
}
class
cat
implements
animal{
public
function
voice(){
echo
miao~~~br;
}
}
class
dog
implements
animal{
public
function
voice(){
echo
wang
~~~br;
}
}
function
dovoice(animal
$obj){
$obj-voice();
}
dovoice(new
dog());
dovoice(new
cat());
② 求代碼示例:php將資料庫讀取出來的文字轉成圖片顯示在頁面上
<?php
$Phone=18907975647;#手機號碼,具體從資料庫怎麼讀出來,你自己寫代碼
$im=imagecreate(300,30);#建立一個寬300,高30像素的圖片對象
imagecolorallocate($im,255,255,255);#將圖片背景填充為白色
$Color=imagecolorallocate($im,0,0,0);#在生成一黑色色顏色,以便寫入字元串
imagestring($im,16,0,0,$Phone,$Color);#將字元串寫到圖片上
header('content-type:image/*');//設置文件頭為圖片格式
imagepng($im);//輸出一個png格式的圖片
imagedestroy($im);//銷毀圖片對象
下面寫效果圖:
③ php獲取本機真實IP地址實例代碼
本文實例為大家分享了php獲取本機真實IP地址實例代碼,供大家參考。
主要是獲取操作系統為win2000/xp、win7的本機IP真實地址,和獲取操作系統為linux類型的本機IP真實地址,具體內容如下
function
getLocalIP()
{
$preg
=
"/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
//獲取操作系統為win2000/xp、win7的本機IP真實地址
exec("ipconfig",
$out,
$stats);
if
(!empty($out))
{
foreach
($out
AS
$row)
{
if
(strstr($row,
"IP")
&&
strstr($row,
":")
&&
!strstr($row,
"IPv6"))
{
$tmpIp
=
explode(":",
$row);
if
(preg_match($preg,
trim($tmpIp[1])))
{
return
trim($tmpIp[1]);
}
}
}
}
//獲取操作系統為linux類型的本機IP真實地址
exec("ifconfig",
$out,
$stats);
if
(!empty($out))
{
if
(isset($out[1])
&&
strstr($out[1],
'addr:'))
{
$tmpArray
=
explode(":",
$out[1]);
$tmpIp
=
explode("
",
$tmpArray[1]);
if
(preg_match($preg,
trim($tmpIp[0])))
{
return
trim($tmpIp[0]);
}
}
}
return
'127.0.0.1';
}
以上就是本文的全部內容,希望對大家的學習有所幫助。
④ php 截取utf-8格式的字元串實例代碼
php
截取utf-8格式的字元串
php中,我們經常需要截取字元串。英文字元佔用一個位元組,中文字元佔用兩個位元組,但中文字元佔用兩個位元組是相對於GBK編碼而言但是在時下國際流行的UTF8編碼中,一個中文字元佔用3個位元組。本文章向大家介紹一個php
截取utf-8格式字元串的函數。
舉例說明:
function
truncate_utf8_string($string,
$length,
$etc
=
'...')
{
$result
=
'';
$string
=
html_entity_decode
(
trim
(
strip_tags
(
$string
)
),
ENT_QUOTES,
'UTF-8'
);
$strlen
=
strlen
(
$string
);
for($i
=
0;
(($i
<
$strlen)
&&
($length
>
0));
$i
++)
{
if
($number
=
strpos
(
str_pad
(
decbin
(
ord
(
substr
(
$string,
$i,
1
)
)
),
8,
'0',
STR_PAD_LEFT
),
'0'
))
{
if
($length
<
1.0)
{
break;
}
$result
.=
substr
(
$string,
$i,
$number
);
$length
-=
1.0;
$i
+=
$number
-
1;
}
else
{
$result
.=
substr
(
$string,
$i,
1
);
$length
-=
0.5;
}
}
$result
=
htmlspecialchars
(
$result,
ENT_QUOTES,
'UTF-8'
);
if
($i
<
$strlen)
{
$result
.=
$etc;
}
return
$result;
}
如果需要截取utf-8格式的字元串,直接調用這個函數即可。
<?php
$str="如果需要截取utf-8格式的字元串,直接調用這個函數即可。";
echo
truncate_utf8_string($str,10);//輸出結果:如果需要截取utf-8格...
?>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
⑤ php幾種排序演算法實例詳解
下面給你介紹四種排序方法:
1) 插入排序(Insertion Sort)的基本思想是:
每次將一個待排序的記錄,按其關鍵字大小插入到前面已經排好序的子文件中的適當位置,直到全部記錄插入完成為止。實現代碼如下:
⑥ 哪位好心人能幫我把9個PHP文件,用asp寫出來。
看了一下,其他幾個沒什麼PHP代碼,直接把裡面的注釋去掉就可以用。我估計別人是不會幫你改的,因為這做事你又不給錢,而且花的時間不是一點點,閑來無事,我對PHP和ASP又比較熟,就抽出幾分鍾幫你改了其中代碼最多的一個文件notify_url.php。
<%
'功能:彩虹易支付非同步通知頁面
'說明:
'以下代碼只是為了方便商戶測試而提供的樣例代碼,商戶可以根據自己網站的需要,按照技術文檔編寫,並非一定要使用該代碼。
'該代碼僅供學習和研究支付寶介面使用,只是提供一個參考。
'*************************頁面功能說明*************************
'創建該頁面文件時,請留心該頁面文件中無任何HTML代碼及空格。
'該頁面不能在本機電腦測試,請到伺服器上做測試。請確保外部可以訪問該頁面。
'該頁面調試工具請使用寫文本函數logResult,該函數已被默認關閉,見alipay_notify_class.php中的函數verifyNotify
<!--#includefile="epay.config.php"-->
<!--#includefile="lib/epay_notify.class.php"-->
'計算得出通知驗證結果
setalipayNotify=newalipay_config
verify_result=alipayNotif.verifyNotify()
ifverify_resultthen '驗證成功
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'請在這里加上商戶的業務邏輯程序代
'——請根據您的業務邏輯來編寫程序(以下代碼僅作參考)——
'獲取支付寶的通知返回參數,可參考技術文檔中伺服器非同步通知參數列表
'商戶訂單號
out_trade_no=Request("out_trade_no")
'彩虹易支付交易號
trade_no=Request("trade_no")
'交易狀態
trade_status=Request("trade_status")
'支付方式
type=Request("type")
ifRequest("trade_status")="TRADE_SUCCESS"then
'判斷該筆訂單是否在商戶網站中已經做過處理
'如果沒有做過處理,根據訂單號(out_trade_no)在商戶網站的訂單系統中查到該筆訂單的詳細,並執行商戶的業務程序
'請務必判斷請求時的total_fee、seller_id與通知時獲取的total_fee、seller_id為一致的
'如果有做過處理,不執行商戶的業務程序
'注意:
'付款完成後,支付寶系統發送該交易狀態通知
endif
'——請根據您的業務邏輯來編寫程序(以上代碼僅作參考)——
response.write"success" '請不要修改或刪除
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
else
'驗證失敗
response.write"fail"
endif
%>
⑦ PHP同時連接多個mysql資料庫示例代碼
這篇文章主要介紹了PHP同時連接多個mysql資料庫的具體實現,需要的朋友可以參考下
實例:
代碼如下:
<?php
$conn1
=
mysql_connect("127.0.0.1",
"root","root","db1");
mysql_select_db("db1",
$conn1);
$conn2
=
mysql_connect("127.0.0.1",
"root","root","db2");
mysql_select_db("db2",
$conn2);
$sql
=
"select
*
from
ip";
$query
=
mysql_query($sql);
if($row
=
mysql_fetch_array($query))
echo
$row[0]."n";
$sql
=
"select
*
from
web
";
$query
=
mysql_query($sql);
if($row
=
mysql_fetch_array($query))
echo
$row[0];
?>
這段代碼存在問題,在程序執行時會報錯:PHP
Warning:
mysql_fetch_array()
expects
parameter
1
to
be
resource,
boolean
given
in
....
原因分析:
程序開始建立兩個資料庫鏈接,函數mysql_query()原型:
resource
mysql_query
(
string
$query
[,
resource
$link_identifier
]
)
向與指定的連接標識符關聯的伺服器中的當前活動資料庫發送一條查詢。如果沒有指定
link_identifier,則使用上一個打開的連接。如果沒有打開的連接,本函數會嘗試無參數調用
mysql_connect()
函數來建立一個連接並使用之。查詢結果會被緩存。
在本例中由於沒有指定link_identifier,所以,在執行第一條sql時,默認使用的是上一個打開的鏈接,即$conn2,而實際上第一條sql語句應該使用的是$conn1,所以導致報錯,所以為了能夠鏈接多個mysql資料庫,可以使用如下方法:
方法1:在mysql_query函數中指定所用連接,即:
代碼如下:
<?php
$conn1
=
mysql_connect("127.0.0.1",
"root","root","db1");
mysql_select_db("Muma",
$conn1);
$conn2
=
mysql_connect("127.0.0.1",
"root","root","db2");
mysql_select_db("proct",
$conn2);
$sql
=
"select
*
from
ip";
$query
=
mysql_query($sql,$conn1);
//添加連接$conn1
if($row
=
mysql_fetch_array($query))
echo
$row[0]."n";
$sql
=
"select
*
from
web
";
$query
=
mysql_query($sql,
$conn2);
if($row
=
mysql_fetch_array($query))
echo
$row[0];
?>
方法2:在sql語句中關聯所用資料庫,此時可以省略mysql_query的第二個參數,即:
代碼如下:
<?php
$conn1
=
mysql_connect("127.0.0.1",
"root","root","db1");
mysql_select_db("db1",
$conn1);
$conn2
=
mysql_connect("127.0.0.1",
"root","root","db2");
mysql_select_db("db2",
$conn2);
$sql
=
"select
*
from
db1.ip";
//關聯資料庫
$query
=
mysql_query($sql);
if($row
=
mysql_fetch_array($query))
echo
$row[0]."n";
$sql
=
"select
*
from
db2.web
";
$query
=
mysql_query($sql);
if($row
=
mysql_fetch_array($query))
echo
$row[0];
?>
⑧ 幾種常用PHP連接資料庫的代碼示例
PHP連接資料庫之PHP連接MYSQL資料庫代碼
<?php
$mysql_server_name='localhost';
//改成自己的mysql資料庫伺服器
$mysql_username='root';
//改成自己的mysql資料庫用戶名
$mysql_password='12345678';
//改成自己的mysql資料庫密碼
$mysql_database='mycounter';
//改成自己的mysql資料庫名
$conn=mysql_connect($mysql_server_name,
$mysql_username,$mysql_password,
$mysql_database);
$sql='CREATEDATABASEmycounter
_chinese_ci;
';
mysql_query($sql);
$sql='CREATETABLE`counter`
(`id`INT(255)UNSIGNEDNOTNULL
AUTO_INCREMENT,`count`INT(255)
UNSIGNEDNOTNULLDEFAULT0,PRIMARYKEY
(`id`))TYPE=innodb;';
mysql_select_db($mysql_database,$conn);
$result=mysql_query($sql);
//echo$sql;
mysql_close($conn);
echo"Hello!資料庫mycounter已經成功建立!";
?>
PHP連接資料庫之PHP連接ACCESS資料庫代碼方法
<?
$conn=newcom("ADODB.Connection");
$connstr="DRIVER={Microsoft
AccessDriver(*.mdb)};
DBQ=".realpath("data/db.mdb");
$conn->Open($connstr);
$rs=newcom("ADODB.RecordSet");
$rs->Open("select*
fromszd_t",$conn,1,1);
while(!$rs->eof){
$f=$rs->Fields(1);
echo$f->value;
$rs->MoveNext();
}
?>
⑨ php實現獲取區域網所有用戶的電腦IP和主機名、及mac地址完整實例
本文所述php實例可以完成獲取區域網所有用戶的電腦IP和主機名、及mac地址的功能,對於php程序設計人員有一定的參考借鑒價值。完整代碼如下:
<?php
$bIp
=
gethostbyname($_ENV['COMPUTERNAME']);
//獲取本機的區域網IP
echo
"本機IP:",$bIp,"\n";
echo
"本機主機名:",gethostbyaddr($bIp),"\n\n\n";
//gethostbyaddr
函數可以根據區域網IP獲取主機名
//默認網關IP
list($ipd1,$ipd2,$ipd3)
=
explode('.',$bIp);
$mask
=
$ipd1
.
"."
.
$ipd2
.
"."
.
$ipd3
;
exec('arp
-a',$aIp);
//獲取區域網中的其他IP
foreach(
$aIp
as
$ipv)
{
if(strpos($ipv,'介面')
!==
false)
{//一下顯示的IP是否是當前區域網中的
而不是其他的類型
可以在cmd下試一下命令
$bool
=
false;
preg_match('/(?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))/',$ipv,$arr);
if(strcmp($arr[0],$bIp)
==
0)
{
$bool
=
true;
}
}
else
{
if($bool)
{
$str
=
preg_replace('/\s+/',
'|',
$ipv);
$sArr
=
explode('|',$str);
if($sArr[1]
==
'Internet'
||
empty($sArr[1]))
{
continue;
}
//去除默認網關
if(strcmp($mask
.
".1",
$sArr[1])
==
0)
{
continue;
}
//去除同網關下255的IP
if(strcmp($mask
.
".255",
$sArr[1])
==
0)
{
continue;
}
//去除組播IP
list($cIp)
=
explode('.',
$sArr[1]);
if($cIp
>=
224
&&
$cIp
<=
239)
{
continue;
}
echo
"IP地址:|",$sArr[1],"|\n";
echo
"MAC地址:",$sArr[2],"\n";
echo
"主機名:",gethostbyaddr($sArr[1]),"\n";
echo
"\n\n";
}
}
}
該程序是在cli模式下運行的,在瀏覽器上應該也可以
php獲取區域網中的用戶ip功能就完成了,主要用到的是php的exec函數
和window的arp
-a
命令
其中獲取本機IP:gethostbyname($_ENV['COMPUTERNAME'])
有別於以往的寫法,感興趣的朋友可以繼續深入研究一下。
獲取主機名函數:gethostbyaddr(IPd)
這個函數功能也很強大。
⑩ PHP等比例壓縮圖片的實例代碼
具體代碼如下所示:
/**
*
desription
壓縮圖片
*
@param
sting
$imgsrc
圖片路徑
*
@param
string
$imgdst
壓縮後保存路徑
*/
public
function
compressedImage($imgsrc,
$imgdst)
{
list($width,
$height,
$type)
=
getimagesize($imgsrc);
$new_width
=
$width;//壓縮後的圖片寬
$new_height
=
$height;//壓縮後的圖片高
if($width
>=
600){
$per
=
600
/
$width;//計算比例
$new_width
=
$width
*
$per;
$new_height
=
$height
*
$per;
}
switch
($type)
{
case
1:
$giftype
=
check_gifcartoon($imgsrc);
if
($giftype)
{
header('Content-Type:image/gif');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromgif($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
}
break;
case
2:
header('Content-Type:image/jpeg');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromjpeg($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
case
3:
header('Content-Type:image/png');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefrompng($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}
總結
以上所述是小編給大家介紹的PHP等比例壓縮圖片的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
您可能感興趣的文章:php中10個不同等級壓縮優化圖片操作示例PHP
實現等比壓縮圖片尺寸和大小實例代碼php
gd等比例縮放壓縮圖片函數基於PHP實現等比壓縮圖片大小php上傳圖片並壓縮的實現方法PHP實現圖片上傳並壓縮PHP實現圖片壓縮的兩則實例php使用imagick模塊實現圖片縮放、裁剪、壓縮示例