ホームページ >php教程 >php手册 >PHP チュートリアル: 初心者向けに学習する価値のあるいくつかの PHP コード スニペット

PHP チュートリアル: 初心者向けに学習する価値のあるいくつかの PHP コード スニペット

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBオリジナル
2016-06-21 08:56:421005ブラウズ

クラシック ループの例



クラシック ループの例

< ; BODY>
for($counter = 1; $counter <= 6; $counter++) //6回ループ
{
print("counter is $ counter
>
for の高度な使用法


for

< ;BODY>
/*
** 必要な説明テキストを出力します
*/
print("月曜日まであと何日ですか?n");
print("

    n");
    for($currentDate = date("U"); //$currentDate の時刻形式を定義します
    date("l" , $currentDate) != "Monday"; // 現在のシステム時間が月曜日かどうかを判断します
    $currentDate += (60 * 60 * 24)) // 現在の時刻に 1 日を加えた値
    {
    / *
    ** 時刻名を表示
    */
    print("
  1. " . date("l", $currentDate) . "n");
    }

    print("
n");
?>



関数の単純な呼び出し:



簡単な関数


function printBold($inputText) //関数 printBold()
{
print("" . $inputText . " ;") ; ////Print $inputText
}
print("この行は強調されていません!
n"); 🎜>
戻り値のある関数


戻り値のある関数</ TITLE><br>< ;/HEAD><br><BODY><br><FONT SIZE=5><br><?<br/> function makeBold($inputText) //関数を定義 makeBold() function <br/> {<br/> $boldedText = "<B>";<br/> $boldedText .= $inputText;<br/> $boldedText .= "</B>";<br/> return($boldedText) ; //Return Variable$ balledText<br/> }<br/> print("この行は強調されていません!!!<BR>n") //文字列を直接出力します <br/> print(makeBold("この行は強調されません)強調されました!!!") . "<BR>n"); //関数 makeBold() を呼び出します function <br/> print("この行は強調されていません!!!<BR>n"); //印刷文字列を直接 <br/>?> ;<br/></SIZE><br></BODY><br></HTML><br><br>デフォルトパラメータを持つ関数<br><HTML> <br><HEAD> <br><TITLE>デフォルトパラメータを持つ関数



function printColored($Text, $Color="black") //関数を定義 function
{
print("$Text"); //文字列の内容と色を取得します
}
printColored("これは黒い単語です!" ;
print("
n");
?>




整数かどうかを判定するアルゴリズム



整数かどうか判定
< /HEAD>

function checkInteger($Number)
{
if($Number > 1)
{
整数のマイナス1 はまだ整数です* /
return(checkInteger($Number-1));
}
elseif($Number <0)
🎜> /* 絶対値を分析できます*/
Return (Checkinteger ((-1)*$ Number-1)); // 絶対値を取得し、整数分析に従って負の数値を分析します
}
Else
{{ {{{{
else
> }
}
print(" 0は整数ですか? " .
" . (7) . "
n");
print("3.5?" . checkInteger(3.5) . "
n");
print("-5 はどうですか?" . checkInteger(-5) . "
n");
print( " . checkInteger(-9.2);
?>

/HTML>
配列の初期化



配列の初期化

< ;FONT SIZE=5>
$monthName = array(1=>"April", "April", "March",//配列を初期化します
"April", "May "、"6 月"、"7 月" 、"8 月"、
"9 月"、"10 月"、"11 月"、"12 月");
print(" 「5 月」は英語で $ monthName[5] .
n");//配列の 6 番目の要素を出力します
?>


配列内の要素を取得します


配列内の要素を取得します

$monthName = array(
/*$monthName[1] を $monthName[12] に定義します*/
1=>"1 月", " 2月」、「3月」、
        "4 月"、"5 月"、"6 月"、
"7 月"、"8 月"、"9 月"、
"10 月"、"11 月"、"12 月"、
/*定义$monthName ["Jan"]到$monthName["Dec"]*/
"Jan"=>"1月"、"2月"=>"2月"、
"3月"=>"3月" 、「Apr」=>「4 月」、
「5 月」=>「5 月」、「6 月」=>「6 月」、
「7 月」=>「7 月」、「8 月」 =>「8 月」、
「9 月」=>「9 月」、「10 月」=>「10 月」、
「11 月」=>「11 月」、「12 月」=>" December",
/*定义$monthName["Jan"]to$monthName["Dec"]*/
"April"=>"1月", "2月"=>"2月",
「March」=>「3 月」、「April」=>「4 月」、
「5 月」=>「5 月」、「6 月」=>「6 月」、
「7 月」 "=>「7 月」、「8 月」=>「8 月」、
「9 月」=>「9 月」、「10 月」=>「10 月」、
「11 月」=> "11 月", "12 月"=>"12 月"
);
/*打印相关的元素*/
print("月 5 です。 " . $monthName[5]. "
n");
print("月 8月" . $monthName["Aug"] . "
n");
print("月 6 月 " . $monthName[" June"] . "
n");
?>


创建一个多维数组


创建一多维数组

$Cities = array( //二维数组array()
"华北地区"=>array(
"北京市",
"天津市",
「石家庄」
),
"西北地区"=>array(
"西安",
"拉萨"
)
);
print( "华北地区: ".$Cities[ "华北地区"][0]); //打印$Cities["华北地区"][0]
?>


PHP 4.0实现表格状打印



实现表格状打印

< ;BODY>
/*
** データ表格化
*/

print("

for($Row=1; $Row <= 12; $Row ++)
{
print(" ;n"); // 開始行

// 各列を実行します
for($Column=1; $列 <= 12; $Column ++)
{
print("
");//开始列
print($Row * $Column);//テーブル要素の積
// 行末

}

print("
n") //表の終わり

?>



<
("使用しているファイルの名前は次のとおりです: ");
print(__FILE__);
print("
n");
print("
");
print("お使いのオペレーティング システムは: ");
print(PHP_OS);
print("
");
print("お使いの php バージョンは: ");
print(PHP_VERSION)
?>



ローカルまたはリモート ファイルを開く



ローカルまたはリモート ファイルを開く</ TITLE><br></HEAD><br><BODY><br><?<br/> print("< ;H3>http プロトコルでファイルを開きます</H3>n");<br> // http プロトコルでファイルを開きます<br> if(!($myFile = fopen("d:web/web/php/test/ data.txt", "r")))<br> {<br> print( "ファイルを開けません"); Content<br> $myLine = fgetss($myFile, 255);<br> print("$ myLine <BR>n");<br> }<br> // ファイルハンドルを閉じます<br> fclose($myFile); <br>?><br></BODY><br/>< /HTML><br>ファイルを開くいくつかの方法の比較<br><HTML><br><HEAD><br><TITLE>ファイルの内容を読み取る


// ファイルを開き、ファイルのすべての文字を出力します
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myCharacter = fgetc($myFile)
print($myCharacter);
}
fclose($myFile) );
}
?>
");?> ;
// ファイルを開いて各行を出力しますファイル
if($myFile = fopen("data.txt", "r"))
{
while( !feof($myFile))

}
? >
");?>
/* ファイルを開き、ファイルの各行を同時に印刷します。
同時に取得した文字列内の HTML 言語を削除します
*/
if($myFile = fopen("data.txt", "r"))
{
while(!feof( $myFile) :) 🎜> }
?> ;


ファイルの共通プロパティにアクセスします



アクセスファイル共通属性




print("ファイル所有者 (UID 値):");
print(fileowner("data.txt")."
");
print("ファイル サイズ:"); print(filesize("data.txt")."
");
print("ファイルの種類:");
print(filetype("data.txt")."
");
?>


テキスト ファイルのコンテンツの呼び出し


< ;TITLE>テキスト ファイルの内容の呼び出し



// 開く 同時に、ファイルの各行を出力します
$myFile = file( "data.txt");
for($index = 0; $index < count($myFile); $index++)
{
print($myFile[$index]."
");
}
?>



カタログ関数の作成


カタログ関数の作成

< ;
if(mkdir("myDir1", 0777)) //ディレクトリを作成する関数
{
print("ディレクトリが正常に作成されました") //ディレクトリが正常に作成されました< 🎜; > }
else
{
print("ディレクトリの作成に失敗しました!") //ディレクトリの作成に失敗しました
}
?>

< ;/HTML>
目次を参照


目次を参照

< ;BODY>
// テーブルを使用してディレクトリの構造を参照します
print("n");
/ / テーブルのヘッダーを作成します
print("n");
print("
n") ;
print("

n");
print("gt;n");
$myDirectory = opendir ("."); // オペレーティングディレクトリへのハンドルを作成します
// ディレクトリ内の各サブエントリを読み取ります
while($entryName = readdir($myDirectory))
{
print ("
") ;
print("
");
print("
");
print("
ファイル名 ファイルのサイズ
$entryName");
print(filesize($entryName)) ;
print("
?>


PHP 関連情報


PHP 関連情報


phpinfo();
?>


一般的に使用される数値判定関数


よく使う数値判定関数<br><br>
//判定配列
$colors = array("red", "blue ", "green");
if(is_array($colors))
{
print ("色は配列です"."
");
}
/ /倍精度数値判定
$Temperature = 15.23;
if(is_double($Temperature))
{
print("温度は double"."
");
}
//整数判定
$PageCount = 2234;
if(is_integer($PageCount) ))
{
print("$PageCount は整数です"."
");
}
//オブジェクト判定
class widget
{
var $name;
var $ length;
}
$thing = new widget;
if(is_object($thing))
{
print("thing はオブジェクトです"."
");
}
//文字判定
$Greeting = "Hello";
if(is_string($Greeting))
{
print("挨拶は文字列です。"< ;br>");
}
?>


ファイル アップロード インターフェイス

< HEAD>
<br/></HEAD><br/><br/><br/>? $UploadAction){<br/>$UploadAction=0; <br/>$TimeLimit=60); <br/>If(($Upfile != "")&& <br/>($Upfile != ""); <br/> {<br/>$ filepath = "d:webwebphptest"; $Upfile_size <(1024 * 1024)) <br/>{<br/>$FileSize =number_format((double)($Upfile_size / 1024), 1) " KB";<br/>} <br/> else <br/> { <br/>$FileSize =number_format((double)($Upfile_size/(1024*1024)),1)."MB" <br/>} <br/>if(!file_exists($FileName)) <br/>{ <br/>if(copy($Upfile,$FileName)) <br/>{unlink($Upfile); <br/>echo "<br><br>n"; $Upfile_name が正常にアップロードされました。 ";<br>echo "<br>gt;<br>n";<br>echo "ファイルの場所: $FileName";<br>echo "<br><br>n";<br>echo "ファイル サイズ: $FileSize";<br>echo "<br><br>n";<br>} <br>else <br>{echo "ファイル $Upfile_name のアップロードに失敗しました。 "; } <br>} <br>else <br>{echo "ファイル $Upfile_name はすでに存在します。 "; } <br>} <br>else <br>{echo "アップロードするファイルが選択されていません。 ";} <e> set_time_limit (30); // デフォルトのタイムアウト設定を復元します <br>} <br>? & Gt; <br> & lt; form enCTYPE =" Multipart/form-data "name =" submitform " <br> アクション = "default.php" メソッド = "POST"> <br><INPUT TYPE = "hidden" NAME = "MAX_FILE_SIZE" VALUE = "1000000"> <br/><INPUT TYPE = "hidden" " NAME = "UploadAction" VALUE = "1"> <br/><TR>><TD><INPUT NAME = "Upfile" TYPE = "file" SIZE = "30"><br/></TD><br/></TR><TR><TD><INPUT NAME = "submit" VALUE = "Submit" TYPE = "submit"><br><input name="reset" value="Reset " type="リセット"><br></e> </td> </tr> <br><br>gt;gt;<br>gt; <br>/ HTML> <br> <br> <br> <br> <br> <p style="width:100%;text-align:center;margin:10px 0"></p></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>声明:</span><div>この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。</div></div></div><div class="nphpSytBox"><span>前の記事:<a class="dBlack" title="PHP サンプル チュートリアル (4): PHP ベースのマイクロブログ サービスの構築" href="https://m.php.cn/ja/faq/236863.html">PHP サンプル チュートリアル (4): PHP ベースのマイクロブログ サービスの構築</a></span><span>次の記事:<a class="dBlack" title="PHP サンプル チュートリアル (4): PHP ベースのマイクロブログ サービスの構築" href="https://m.php.cn/ja/faq/236866.html">PHP サンプル チュートリアル (4): PHP ベースのマイクロブログ サービスの構築</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>関連記事</h2><em><a href="https://m.php.cn/ja/article.html" class="bBlack"><i>続きを見る</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/ja/faq/92233.html" title="Windows7系统下Netbeans+PHPUnit搭建PHP单元测试开发环境及PHPUn" class="aBlack">Windows7系统下Netbeans+PHPUnit搭建PHP单元测试开发环境及PHPUn</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/ja/faq/97151.html" title="程序猿ProMonkey V2.03" class="aBlack">程序猿ProMonkey V2.03</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/ja/faq/94770.html" title="PHP Navigator" class="aBlack">PHP Navigator</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/ja/faq/90182.html" title="php返回数组中指定的一列(php5.5.0默认函数array_column()在php" class="aBlack">php返回数组中指定的一列(php5.5.0默认函数array_column()在php</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/ja/faq/208396.html" title="htmlentitiesとhtmlspecialcharsの違いの詳しい説明" class="aBlack">htmlentitiesとhtmlspecialcharsの違いの詳しい説明</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!</p></div><div class="footermid"><a href="https://m.php.cn/ja/about/us.html">私たちについて</a><a href="https://m.php.cn/ja/about/disclaimer.html">免責事項</a><a href="https://m.php.cn/ja/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>