Created
March 18, 2019 06:40
-
-
Save sezemiadmin/d5de882b7ed6a06a4847116b6333102e to your computer and use it in GitHub Desktop.
1日で習得するPHP入門 サンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function foo($callback) { //変数名は何でもOKです | |
$callback(); // => OK | |
} | |
foo(function() { | |
echo "OK"; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = [1, 2, 3]; | |
foreach ($a as $key => $value) { //連想配列 | |
echo "{$key} : {$value}\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ composer -v | |
______ | |
/ ____/___ ____ ___ ____ ____ ________ _____ | |
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ | |
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / | |
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ | |
/_/ | |
Composer version @package_branch_alias_version@ (1.0.0-beta2) 2016-03-27 16:00:34 | |
Usage: | |
command [options] [arguments] | |
(中略) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = array(); | |
$a = array(1, 2, 3); | |
$a = [1, 2, 3]; // 簡易的な書き方も出来ます | |
echo $a[0]; // 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Person { | |
private $name; // instance field | |
public function setName($name) { // instance method | |
$this->name = $name; | |
} | |
public function getName() { // instance method | |
return $this->name; | |
} | |
public static function getClassName() { // static method | |
return "Person: "; | |
} | |
} | |
class Employee extends Person { | |
} | |
$e = new Employee(); | |
$e->setName("Eric"); | |
echo $e->getName(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = [1, 2, 3]; | |
for ($i = 0; $i < count($a); $i++) { | |
echo $a[$i]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = [1, 2, 3]; | |
foreach($a as $num) { | |
echo $num; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>数当てゲーム</title> | |
</head> | |
<body> | |
<h4>1~100の数字を入力</h4> | |
<form action="number-guess.php" method="post"> | |
<input type="number" min="1" max="100" name="num" size="4" | |
autofocus required /> | |
<input type="submit" value="Go!" /> | |
</form> | |
<?php | |
session_start(); | |
if (empty($_SESSION['cnt'])) { // 初めてページを表示した場合 | |
$_SESSION['cnt'] = 1; // セッションに「cnt」という名前で値1を設定 (カウンター) | |
$_SESSION['ans'] = rand(1, 100); | |
} else { // 2回目以降の表示 | |
if (isset($_POST['num'])) { | |
$num = $_POST['num']; | |
printf("%s回目: %s", $_SESSION['cnt']++, $num); | |
$ans = $_SESSION['ans']; // 正解の値をセッションから取り出して変数ansに格納 | |
if ($num == $ans) { // 入力された値と正解の値が等しい場合 | |
echo " 正解!"; | |
} | |
if ($num > $ans) { // 入力された値が正解の値よりも大きい場合 | |
echo " 大きすぎ"; | |
} | |
if ($num < $ans) { // 入力された値が正解の値よりも小さい場合 | |
echo " 小さすぎ"; | |
} | |
} | |
} | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Greeting</title> | |
<link rel="stylesheet" href="{{ asset('css/app.css') }}"> | |
</head> | |
<body> | |
<h1>Hello, {{ $name }}</h1> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "Hello, PHP!!\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<h1><?php echo "Hello, PHP!"; ?></h1> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php > echo "Hello, PHP!!"; | |
Hello, PHP!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = 2; | |
$b = 1; | |
if ($a > $b) | |
echo "aはbより大きい\n"; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ Laravel new laravel-sample | |
: | |
: | |
: | |
Generating optimized autoload files | |
> @php -r "file_exists('.env') || copy('.env.example', '.env');" | |
> @php artisan key:generate --ansi | |
Application key set successfully. | |
> Illuminate\Foundation\ComposerScripts::postAutoloadDump | |
> @php artisan package:discover --ansi | |
Discovered Package: beyondcode/laravel-dump-server | |
Discovered Package: fideloper/proxy | |
Discovered Package: laravel/nexmo-notification-channel | |
Discovered Package: laravel/slack-notification-channel | |
Discovered Package: laravel/tinker | |
Discovered Package: nesbot/carbon | |
Discovered Package: nunomaduro/collision | |
Package manifest generated successfully. | |
Application ready! Build something amazing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (1 == "1") { | |
echo "OK" | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Person { | |
private $name; // instance field | |
public function setName($name) { // instance method | |
$this->name = $name; | |
} | |
public function getName() { // instance method | |
return $this->name; | |
} | |
public static function getClassName() { // static method | |
return "Person: "; | |
} | |
} | |
echo Person::getClassName(); // => Person:(static methodの利用) | |
$p = new Person(); // クラスからオブジェクトを生成 | |
$p->setName("John"); // 名前の設定(instance methodの利用) | |
echo $p->getName(); // => John(名前の取得) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php > var_dump(1); | |
int(1) | |
php > var_dump(1.0); | |
float(1) | |
php > var_dump('a'); | |
string(1) "a" | |
php > var_dump(true); | |
bool(true) | |
php > var_dump("abcd"); | |
string(4) "abcd" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sum($a, $b) { | |
return $a + $b; | |
} | |
echo sum(1, 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>数当てゲーム</title> | |
</head> | |
<body> | |
<h4>1~100の数字を入力</h4> | |
<form action="number-guess.php" method="post"> | |
<input type="number" min="1" max="100" name="num" size="4" | |
autofocus required /> | |
<input type="submit" value="Go!" /> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>数当てゲーム</title> | |
</head> | |
<body> | |
<h4>1~100の数字を入力</h4> | |
<form action="number-guess.php" method="post"> | |
<input type="number" min="1" max="100" name="num" size="4" | |
autofocus required /> | |
<input type="submit" value="Go!" /> | |
</form> | |
<?php | |
if (isset($_POST['num'])) { // POSTリクエストにnumパラメーターが含まれている場合 | |
$num = $_POST['num']; // パラメーターを取得 | |
printf($num); // 表示 | |
} | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>数当てゲーム</title> | |
</head> | |
<body> | |
<h4>1~100の数字を入力</h4> | |
<form action="number-guess.php" method="post"> | |
<input type="number" min="1" max="100" name="num" size="4" | |
autofocus required /> | |
<input type="submit" value="Go!" /> | |
</form> | |
<?php | |
session_start(); | |
if (empty($_SESSION['cnt'])) { // 初めてページを表示した場合 | |
$_SESSION['cnt'] = 1; // セッションに「cnt」という名前で値1を設定 (カウンタ) | |
} else { // 2回目以降の表示 | |
if (isset($_POST['num'])) { | |
$num = $_POST['num']; | |
printf("%s回目: %s", $_SESSION['cnt']++, $num); | |
} | |
} | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Laravel の初期ページ | |
Route::get('/', function () { | |
return view('welcome'); | |
}); | |
// 以下に /hello がリクエストされたら greeting.blade.php を返すよう追加 | |
Route::get('/hello', function() { | |
return view('greeting', ['name' => 'Laravel']); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment