{"id":1163,"date":"2025-07-06T11:42:06","date_gmt":"2025-07-06T02:42:06","guid":{"rendered":"https:\/\/blog.espresso.earth\/?p=1163"},"modified":"2025-07-09T23:54:42","modified_gmt":"2025-07-09T14:54:42","slug":"php","status":"publish","type":"post","link":"https:\/\/blog.espresso.earth\/index.php\/2025\/07\/06\/php\/","title":{"rendered":"PHP\u5165\u9580\uff1aWeb\u958b\u767a\u306e\u7b2c\u4e00\u6b69"},"content":{"rendered":"\n<p>PHP\u306f\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3067\u52d5\u4f5c\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8\u8a00\u8a9e\u3067\u3001\u4e3b\u306bWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u69cb\u7bc9\u306b\u5229\u7528\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001PHP\u306e\u57fa\u672c\u69cb\u6587\u304b\u3089\u5236\u5fa1\u69cb\u6587\u3001\u30af\u30e9\u30b9\u306e\u4f7f\u3044\u65b9\u3001\u30d5\u30a9\u30fc\u30e0\u51e6\u7406\u3001\u305d\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u307e\u3067\u3001\u57fa\u790e\u3092\u4e00\u901a\u308a\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc 1. PHP\u306e\u57fa\u672c\u69cb\u6587<\/h2>\n\n\n\n<p>PHP\u306e\u30b3\u30fc\u30c9\u306f <code>&lt;?php<\/code> \u3067\u59cb\u307e\u308a\u3001<code>?&gt;<\/code> \u3067\u9589\u3058\u307e\u3059\u3002HTML\u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u4f7f\u3046\u3053\u3068\u304c\u591a\u304f\u3001\u6b21\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\necho \"\u3053\u3093\u306b\u3061\u306f\u3001PHP!\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd01 2. for\u6587\u3068foreach\u6587\u306e\u9055\u3044<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><code>for<\/code> \u6587\uff1a\u6570\u5024\u306e\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u306b\u9069\u3057\u3066\u3044\u308b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for ($i = 0; $i &lt; 5; $i++) {\n    echo $i . \"&lt;br>\";\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$colors = &#91;\"red\" =&gt; \"#FF0000\", \"green\" =&gt; \"#00FF00\"];\nforeach ($colors as $name =&gt; $hex) {\n    echo \"$name: $hex&lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>foreach<\/code> \u6587\uff1a\u914d\u5217\u306e\u8981\u7d20\u3092\u7c21\u6f54\u306b\u51e6\u7406\u3067\u304d\u308b<\/h3>\n\n\n\n<p>\ud83d\udd0d <code>foreach<\/code> \u306f <strong>\u30ad\u30fc\u3068\u5024\u306e\u4e21\u65b9<\/strong> \u3082\u53d6\u308a\u51fa\u305b\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$fruits = &#91;\"apple\", \"banana\", \"orange\"];\nforeach ($fruits as $fruit) {\n    echo $fruit . \"&lt;br>\";\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 3. \u30af\u30e9\u30b9\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h2>\n\n\n\n<p>PHP\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306b\u3082\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002\u57fa\u672c\u7684\u306a\u30af\u30e9\u30b9\u306e\u5b9a\u7fa9\u3068\u4f7f\u3044\u65b9\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car {\n    public $color;\n\n    public function __construct($color) {\n        $this-&gt;color = $color;\n    }\n\n    public function drive() {\n        echo \"This car is \" . $this-&gt;color . \" and it's driving!\";\n    }\n}\n\n$myCar = new Car(\"blue\");\n$myCar-&gt;drive();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf10 4. \u30b9\u30fc\u30d1\u30fc\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\uff08\u30d6\u30e9\u30a6\u30b6\u3084\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u60c5\u5831\uff09<\/h2>\n\n\n\n<p>PHP\u306b\u306f\u69d8\u3005\u306a <strong>\u30b9\u30fc\u30d1\u30fc\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570<\/strong> \u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5909\u6570\u540d<\/th><th>\u8aac\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>$_GET<\/code><\/td><td>URL\u306e\u30af\u30a8\u30ea\u6587\u5b57\u5217\u3067\u9001\u3089\u308c\u305f\u30c7\u30fc\u30bf<\/td><\/tr><tr><td><code>$_POST<\/code><\/td><td>\u30d5\u30a9\u30fc\u30e0\u306ePOST\u9001\u4fe1\u3067\u9001\u3089\u308c\u305f\u30c7\u30fc\u30bf<\/td><\/tr><tr><td><code>$_SERVER<\/code><\/td><td>\u30b5\u30fc\u30d0\u30fc\u3084\u5b9f\u884c\u74b0\u5883\u306e\u60c5\u5831\uff08IP\u3084User-Agent\uff09<\/td><\/tr><tr><td><code>$_SESSION<\/code><\/td><td>\u30bb\u30c3\u30b7\u30e7\u30f3\u306b\u4fdd\u5b58\u3055\u308c\u305f\u30c7\u30fc\u30bf<\/td><\/tr><tr><td><code>$_COOKIE<\/code><\/td><td>\u30d6\u30e9\u30a6\u30b6\u306b\u4fdd\u5b58\u3055\u308c\u305f\u30af\u30c3\u30ad\u30fc<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\uff1aGET\u306e\u4f7f\u3044\u65b9<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ URL: example.com\/page.php?name=Taro\n$name = $_GET&#91;'name'] ?? 'Guest';\necho \"\u3053\u3093\u306b\u3061\u306f\u3001\" . htmlspecialchars($name);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">POST\u306e\u4f7f\u3044\u65b9\uff08HTML\u30d5\u30a9\u30fc\u30e0\u3068\u30bb\u30c3\u30c8\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form method=\"post\"&gt;\n    &lt;input type=\"text\" name=\"email\"&gt;\n    &lt;input type=\"submit\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$email = $_POST&#91;'email'] ?? '';\necho \"\u9001\u4fe1\u3055\u308c\u305f\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9: \" . htmlspecialchars($email);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\uddc3\ufe0f 5. \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\uff08PDO\u306b\u3088\u308b\u63a5\u7d9a\u3068\u64cd\u4f5c\uff09<\/h2>\n\n\n\n<p>PHP\u3067DB\u64cd\u4f5c\u3092\u884c\u3046\u969b\u306f <strong>PDO\uff08PHP Data Objects\uff09<\/strong> \u3092\u4f7f\u3046\u3068\u5b89\u5168\u3067\u67d4\u8edf\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u63a5\u7d9a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    $pdo = new PDO(\"mysql:host=localhost;dbname=testdb;charset=utf8\", \"username\", \"password\");\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n} catch (PDOException $e) {\n    echo \"\u63a5\u7d9a\u5931\u6557: \" . $e-&gt;getMessage();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\uff08SELECT\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$stmt = $pdo-&gt;query(\"SELECT * FROM users\");\nforeach ($stmt as $row) {\n    echo $row&#91;'name'] . \"&lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b\uff08INSERT\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$stmt = $pdo-&gt;prepare(\"INSERT INTO users (name, email) VALUES (?, ?)\");\n$stmt-&gt;execute(&#91;\"Taro\", \"taro@example.com\"]);<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <code>prepare()<\/code> \u3068 <code>execute()<\/code> \u3092\u4f7f\u3046\u3053\u3068\u3067SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u5bfe\u7b56\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u270d\ufe0f \u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u4ee5\u4e0b\u306ePHP\u306e\u57fa\u790e\u77e5\u8b58\u3092\u30ab\u30d0\u30fc\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u57fa\u672c\u69cb\u6587\u3068\u30b3\u30e1\u30f3\u30c8\u306e\u66f8\u304d\u65b9<\/li>\n\n\n\n<li><code>for<\/code> \u3068 <code>foreach<\/code> \u306e\u4f7f\u3044\u5206\u3051<\/li>\n\n\n\n<li>\u30af\u30e9\u30b9\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411<\/li>\n\n\n\n<li>\u30b9\u30fc\u30d1\u30fc\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u51e6\u7406<\/li>\n\n\n\n<li>PDO\u3092\u4f7f\u3063\u305f\u5b89\u5168\u306aDB\u64cd\u4f5c<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u306f\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3067\u52d5\u4f5c\u3059\u308b\u30b9\u30af\u30ea\u30d7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1446,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,39,24],"tags":[88],"class_list":["post-1163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-it","category-php","category-web","tag-php"],"_links":{"self":[{"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/posts\/1163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/comments?post=1163"}],"version-history":[{"count":12,"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/posts\/1163\/revisions"}],"predecessor-version":[{"id":1278,"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/posts\/1163\/revisions\/1278"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/media\/1446"}],"wp:attachment":[{"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/media?parent=1163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/categories?post=1163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.espresso.earth\/index.php\/wp-json\/wp\/v2\/tags?post=1163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}