{"id":25470310,"url":"https://github.com/thinkphp/computer-science-in-php","last_synced_at":"2026-01-27T02:33:55.441Z","repository":{"id":66902969,"uuid":"42389985","full_name":"thinkphp/computer-science-in-php","owner":"thinkphp","description":"Computer Science in PHP language","archived":false,"fork":false,"pushed_at":"2024-09-26T08:37:29.000Z","size":193,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-17T15:09:59.560Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thinkphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-09-13T09:13:21.000Z","updated_at":"2024-09-26T08:40:23.000Z","dependencies_parsed_at":"2025-02-18T08:43:38.344Z","dependency_job_id":null,"html_url":"https://github.com/thinkphp/computer-science-in-php","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thinkphp/computer-science-in-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thinkphp","download_url":"https://codeload.github.com/thinkphp/computer-science-in-php/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-php/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28796977,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T01:07:07.743Z","status":"online","status_checked_at":"2026-01-27T02:00:07.755Z","response_time":168,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-18T08:32:53.279Z","updated_at":"2026-01-27T02:33:55.435Z","avatar_url":"https://github.com/thinkphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction to PHP Coursework\n\n## Table of Contents\n1. [Introduction to PHP](#1-introduction-to-php)\n2. [Variables and Data Types](#2-variables-and-data-types)\n3. [Control Structures](#3-control-structures)\n4. [Functions](#4-functions)\n5. [Arrays](#5-arrays)\n6. [Superglobals and Forms](#6-superglobals-and-forms)\n7. [Working with Files](#7-working-with-files)\n8. [Object-Oriented Programming](#8-object-oriented-programming)\n9. [Database Interaction](#9-database-interaction)\n10. [Error Handling](#10-error-handling)\n\n## 1. Introduction to PHP\n\nPHP (Hypertext Preprocessor) is a server-side scripting language designed for web development but also used as a general-purpose programming language.\n\n### 1.1 Basic Syntax\n```php\n\u003c?php\n// Example 1: Hello World\necho \"Hello, World!\";\n\n// Example 2: PHP info\nphpinfo();\n\n// Example 3: Embedding PHP in HTML\n?\u003e\n\u003chtml\u003e\n\u003cbody\u003e\n    \u003ch1\u003e\u003c?php echo \"Welcome to PHP!\"; ?\u003e\u003c/h1\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n## 2. Variables and Data Types\n\n### 2.1 Variables\n```php\n\u003c?php\n// Example 1: Variable declaration and assignment\n$name = \"John\";\n$age = 25;\necho \"My name is $name and I am $age years old.\";\n\n// Example 2: Variable scope\n$x = 5; // global scope\nfunction myTest() {\n    $y = 10; // local scope\n    global $x; // accessing global variable\n    echo \"x is $x and y is $y\";\n}\nmyTest();\n\n// Example 3: Static variables\nfunction counter() {\n    static $count = 0;\n    echo $count;\n    $count++;\n}\ncounter(); // Output: 0\ncounter(); // Output: 1\ncounter(); // Output: 2\n?\u003e\n```\n\n### 2.2 Data Types\n```php\n\u003c?php\n// Example 1: Numeric types\n$int_var = 42;\n$float_var = 3.14;\necho gettype($int_var) . \", \" . gettype($float_var);\n\n// Example 2: String\n$str = \"Hello, PHP!\";\necho strlen($str); // Output: 11\n\n// Example 3: Boolean\n$is_student = true;\necho $is_student ? \"Yes\" : \"No\"; // Output: Yes\n?\u003e\n```\n\n## 3. Control Structures\n\n### 3.1 Conditional Statements\n```php\n\u003c?php\n// Example 1: If-else statement\n$age = 18;\nif ($age \u003e= 18) {\n    echo \"You are an adult.\";\n} else {\n    echo \"You are a minor.\";\n}\n\n// Example 2: Switch statement\n$day = \"Monday\";\nswitch ($day) {\n    case \"Monday\":\n        echo \"It's the start of the week.\";\n        break;\n    case \"Friday\":\n        echo \"Weekend is near!\";\n        break;\n    default:\n        echo \"It's a regular day.\";\n}\n\n// Example 3: Ternary operator\n$score = 75;\n$result = ($score \u003e= 60) ? \"Pass\" : \"Fail\";\necho $result;\n?\u003e\n```\n\n### 3.2 Loops\n```php\n\u003c?php\n// Example 1: For loop\nfor ($i = 1; $i \u003c= 5; $i++) {\n    echo $i . \" \";\n}\n\n// Example 2: While loop\n$counter = 0;\nwhile ($counter \u003c 5) {\n    echo \"Count: $counter \";\n    $counter++;\n}\n\n// Example 3: Foreach loop\n$colors = [\"red\", \"green\", \"blue\"];\nforeach ($colors as $color) {\n    echo $color . \" \";\n}\n?\u003e\n```\n\n## 4. Functions\n\n### 4.1 User-defined Functions\n```php\n\u003c?php\n// Example 1: Simple function\nfunction greet($name) {\n    return \"Hello, $name!\";\n}\necho greet(\"Alice\");\n\n// Example 2: Function with default parameter\nfunction power($base, $exponent = 2) {\n    return pow($base, $exponent);\n}\necho power(3); // Output: 9\necho power(2, 3); // Output: 8\n\n// Example 3: Variable-length argument lists\nfunction sum(...$numbers) {\n    return array_sum($numbers);\n}\necho sum(1, 2, 3, 4, 5); // Output: 15\n?\u003e\n```\n\n## 5. Arrays\n\n### 5.1 Array Manipulation\n```php\n\u003c?php\n// Example 1: Indexed arrays\n$fruits = [\"apple\", \"banana\", \"cherry\"];\necho $fruits[1]; // Output: banana\n\n// Example 2: Associative arrays\n$person = [\n    \"name\" =\u003e \"John\",\n    \"age\" =\u003e 30,\n    \"city\" =\u003e \"New York\"\n];\necho $person[\"name\"]; // Output: John\n\n// Example 3: Multidimensional arrays\n$matrix = [\n    [1, 2, 3],\n    [4, 5, 6],\n    [7, 8, 9]\n];\necho $matrix[1][1]; // Output: 5\n?\u003e\n```\n\n## 6. Superglobals and Forms\n\n### 6.1 Working with Forms\n```php\n\u003c!-- Example 1: HTML Form --\u003e\n\u003cform method=\"post\" action=\"process.php\"\u003e\n    \u003cinput type=\"text\" name=\"username\"\u003e\n    \u003cinput type=\"submit\" value=\"Submit\"\u003e\n\u003c/form\u003e\n\n\u003c?php\n// process.php\n// Example 2: Accessing form data\nif ($_SERVER[\"REQUEST_METHOD\"] == \"POST\") {\n    $username = $_POST[\"username\"];\n    echo \"Hello, $username!\";\n}\n\n// Example 3: Using GET parameters\n// Assuming URL: page.php?id=123\u0026action=view\n$id = $_GET[\"id\"];\n$action = $_GET[\"action\"];\necho \"ID: $id, Action: $action\";\n?\u003e\n```\n\n## 7. Working with Files\n\n### 7.1 File Operations\n```php\n\u003c?php\n// Example 1: Reading a file\n$content = file_get_contents(\"example.txt\");\necho $content;\n\n// Example 2: Writing to a file\n$data = \"This is some content.\";\nfile_put_contents(\"output.txt\", $data);\n\n// Example 3: Appending to a file\n$newData = \"This is additional content.\";\nfile_put_contents(\"output.txt\", $newData, FILE_APPEND);\n?\u003e\n```\n\n## 8. Object-Oriented Programming\n\n### 8.1 Classes and Objects\n```php\n\u003c?php\n// Example 1: Simple class\nclass Car {\n    public $brand;\n    public function __construct($brand) {\n        $this-\u003ebrand = $brand;\n    }\n    public function getBrand() {\n        return $this-\u003ebrand;\n    }\n}\n$myCar = new Car(\"Toyota\");\necho $myCar-\u003egetBrand(); // Output: Toyota\n\n// Example 2: Inheritance\nclass ElectricCar extends Car {\n    public $batteryLife;\n    public function __construct($brand, $batteryLife) {\n        parent::__construct($brand);\n        $this-\u003ebatteryLife = $batteryLife;\n    }\n}\n$tesla = new ElectricCar(\"Tesla\", \"300 miles\");\necho $tesla-\u003egetBrand() . \" - \" . $tesla-\u003ebatteryLife;\n\n// Example 3: Static methods\nclass MathOperations {\n    public static function add($a, $b) {\n        return $a + $b;\n    }\n}\necho MathOperations::add(5, 3); // Output: 8\n?\u003e\n```\n\n## 9. Database Interaction\n\n### 9.1 MySQL Database Operations\n```php\n\u003c?php\n// Example 1: Connecting to a database\n$conn = new mysqli(\"localhost\", \"username\", \"password\", \"database\");\nif ($conn-\u003econnect_error) {\n    die(\"Connection failed: \" . $conn-\u003econnect_error);\n}\n\n// Example 2: Inserting data\n$sql = \"INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')\";\nif ($conn-\u003equery($sql) === TRUE) {\n    echo \"New record created successfully\";\n}\n\n// Example 3: Selecting data\n$sql = \"SELECT id, name, email FROM users\";\n$result = $conn-\u003equery($sql);\nif ($result-\u003enum_rows \u003e 0) {\n    while($row = $result-\u003efetch_assoc()) {\n        echo \"ID: \" . $row[\"id\"]. \" - Name: \" . $row[\"name\"]. \" - Email: \" . $row[\"email\"]. \"\u003cbr\u003e\";\n    }\n}\n$conn-\u003eclose();\n?\u003e\n```\n\n## 10. Error Handling\n\n### 10.1 Exception Handling\n```php\n\u003c?php\n// Example 1: Try-catch block\ntry {\n    $result = 10 / 0;\n} catch (DivisionByZeroError $e) {\n    echo \"Error: \" . $e-\u003egetMessage();\n}\n\n// Example 2: Custom exception\nclass CustomException extends Exception {}\ntry {\n    throw new CustomException(\"This is a custom exception\");\n} catch (CustomException $e) {\n    echo \"Caught exception: \" . $e-\u003egetMessage();\n}\n\n// Example 3: Finally block\ntry {\n    // Some code that might throw an exception\n} catch (Exception $e) {\n    echo \"Caught exception: \" . $e-\u003egetMessage();\n} finally {\n    echo \"This will always execute\";\n}\n?\u003e\n```\n\nThis coursework covers the fundamental concepts of PHP, including basic syntax, variables, control structures, functions, arrays, form handling, file operations, object-oriented programming, database interaction, and error handling. Each concept is explained with at least three examples to reinforce understanding.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkphp%2Fcomputer-science-in-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthinkphp%2Fcomputer-science-in-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkphp%2Fcomputer-science-in-php/lists"}