{"id":25460993,"url":"https://github.com/macamer/php","last_synced_at":"2026-06-21T16:31:00.897Z","repository":{"id":231496581,"uuid":"781889322","full_name":"macamer/Php","owner":"macamer","description":"Learning php","archived":false,"fork":false,"pushed_at":"2025-03-21T15:03:16.000Z","size":72,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-23T17:43:51.465Z","etag":null,"topics":["ajax","php"],"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/macamer.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":"2024-04-04T08:31:37.000Z","updated_at":"2025-07-02T14:34:39.000Z","dependencies_parsed_at":"2024-04-04T10:26:45.639Z","dependency_job_id":"a996875b-189b-4e44-aff9-418e8bacca9c","html_url":"https://github.com/macamer/Php","commit_stats":null,"previous_names":["macamer/php"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/macamer/Php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macamer%2FPhp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macamer%2FPhp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macamer%2FPhp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macamer%2FPhp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/macamer","download_url":"https://codeload.github.com/macamer/Php/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macamer%2FPhp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34618474,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-21T02:00:05.568Z","response_time":54,"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":["ajax","php"],"created_at":"2025-02-18T05:05:43.276Z","updated_at":"2026-06-21T16:31:00.880Z","avatar_url":"https://github.com/macamer.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"./php.png\" style=\"height: 8%; width:8%;\"/\u003e\n\n\n## Launch the website\nRun on localhost\n```\nphp -S localhost:8000\ncltr + c\n```\nTo see website printed on terminal\n```\nphp file.php\n```\n\u003cbr/\u003e\n\n## Variables\nVariables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.\n\nVariable declaration rules:\n\n1. Start with dollar sign($)\n2. First letter of variable name comes from a-zA-z_\n3. Next letters of variable name comes from a-zA-Z0-9_\n4. No space,no syntex\n\n**Variables**\n```\n$name = \"Maria\"; \n```\n**Constant** \\\nWhitout $ and with uppercase\n```\nconst NOMBRE = 'Maria';\n```\n**Global Constant**\n```\ndefine('LOGO_URL', 'https://cdn.freebiesupply.com/logos/large/2x/php-1-logo-svg-vector.svg'); \n```\n**Concatenation**\n```\n$num = 39;\n$newNum = $num  . \"1\";\n```\nPHP is a **dynamically typed** language, which means that by default there is no need to specify the type of a variable, as this will be determined at runtime.\nYou can define the variables by putting the type before the name.\n\n*int $number*\n\n**To enable strict type for typed data**\n```\ndeclare(strict_types=1);\n```\n\u003e [!WARNING]\n\u003e It only affect the file and it has to be on the first line of the file\n\u003cbr/\u003e\n\n## Basic functions\n| Function  | Example | Explanation  |\n| ------------- | ------------- | ------------|\n| **gettype()**  | [echo gettype($ageBool);](02-conceptos.php) | get the type of the variable |\n| **implode()**  | [implode(\", \", $array)](06-classes/classes.php) | convert an array into a string |\n| **array_rand()**  | [$names[array_rand($names)]](06-classes/classes.php)  | Picks one or more **random** entries out of an array, and returns the key (or keys) of the random entries. |\n| **extract(*variable*)**  | [extract($data);](05-importstructure/functions.php)  | Extracts the data and transform it into variables. Instead of array you have a variable. For example title instead of $data['title] |\n| **array_merge(*array,array2*)**  | [array_merge($data,['until_message'=\u003e$until_message]);](05-importstructure/index.php)  | Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. |\n| **json_decore(*variable*)**  | [$data = json_decode($result, true);](05-importstructure/functions.php)  | When **true**, JSON objects will be returned as associative *arrays*; when **false**, JSON objects will be returned as *objects*. |\n| **file_get_contents(*file*)**  | [$result = file_get_contents(API_URL);](05-importstructure/functions.php)  |Reads entire file into a string|\n\n\n\u003cbr/\u003e\n\n## Classes\nAtributes \u0026 Constructor\n```\npublic $name;\npublic $powers;\npublic $planet;\n\npublic function __construct($name, $powers, $planet) {\n  $this-\u003ename = $name;\n  $this-\u003epowers = $powers;\n  $this-\u003eplanet = $planet;\n}\n```\n\u003e [!TIP]\n\u003e With PHP 8 you can use this\n\u003e```\n\u003epublic function __construct(\n\u003e        public string $name, \n\u003e        public array $powers, \n\u003e        public string $planet,\n\u003e    ) {}\n\u003e```\n**Static Method** \\\nStatic methods are callable without an instance of the object created. \\\nIn order to use an static method this is the structure:\n```\nSuperHero::random();\n```\n*Example [classes.php](06-classes/classes.php)* \\\n**Public Method** \\\nPublic methods are callable with an instance of the object created. \\\nIn order to use a public method this is the structure:\n```\n$hero = new SuperHero(\"Superman\", [\"Volar\", \"Supervista\", \"Fuerza\"], \"Krypton\");\n$hero-\u003edescription();\n```\n*Example [classes.php](06-classes/classes.php)* \\\n**Functions**\n| Function  | Example | Explanation  |\n| ------------- | ------------- | ------------|\n| get_object_vars()  | [get_object_vars($this)](06-classes/classes.php)  | Returns an associative array of defined object accessible non-static properties for the specified object in scope. Interesting in order to see information of an object |\n\n## AJAX with PHP\nUse Javascript to conect with web server.\nThe followings lines are a basic structure of ajax:\n```\nvar xmlhttp = new XMLHttpRequest();\n    xmlhttp.onreadystatechange = function() {\n      if (this.readyState == 4 \u0026\u0026 this.status == 200) {\n        document.getElementById(\"txtHint\").innerHTML = this.responseText;\n      }\n    };\n    xmlhttp.open(\"GET\", \"gethint.php?q=\" + str, true);\n    xmlhttp.send();\n```\n\nWeb site conect to *gethint.php* and search variable called *q*.\n```\n//gethint.php\n$q = $_REQUEST[\"q\"];\n\n$hint = \"\";\n\n// lookup all hints from array if $q is different from \"\"\nif ($q !== \"\") {\n  $q = strtolower($q);\n  $len=strlen($q);\n  foreach($a as $name) {\n    if (stristr($q, substr($name, 0, $len))) {\n      if ($hint === \"\") {\n        $hint = $name;\n      } else {\n        $hint .= \", $name\";\n      }\n    }\n  }\n}\n```\n*Example [08-basic-ajax](08-basic-ajax/index.html)*\n\n|Property\t|Description|\n|---------|-----------|\n|onreadystatechange\t|Defines a function to be called when the readyState property changes|\n|readyState\t|Holds the status of the XMLHttpRequest.|\n||0: request not initialized|\n||1: server connection established|\n||2: request received|\n||3: processing request||\n||4: request finished and response is ready|\n|status |\t200: \"OK\"|\n||403: \"Forbidden\"|\n||404: \"Page not found\"|\n|statusText|\tReturns the status-text (e.g. \"OK\" or \"Not Found\")*onreadystatechange* is an event that run every time the readyState of the request changes.|\n\n**setRequestHeader()** \\\nThe XMLHttpRequest method setRequestHeader() sets the value of an HTTP request header. When using setRequestHeader(), you must call it after calling *open()*, but before calling *send()*. If this method is called several times with the same header, the values are merged into one single request header.\n```\nxmlhttprequest.setRequestHeader(\n    'Content-Type',\n    'application/x-www-form-urlencoded'\n);\n```\n\n**Send data to PHP** \\\nWhen you want to send the data from your website to the server.\n```\ntheObject.send('username=Maria');\n```\nThen you can check if the data is empty or verify the information.\n```\n\u003c?php\nif (isset($_POST['username'])) {\n    echo \"Usuario: \" . $_POST['username'];\n} else {\n    echo \"No se ingresó ningún usuario.\";\n}\n```\n| Function  | Example | Explanation  |\n| ------------- | ------------- | ------------|\n| **isset()**  | [isset($_POST['username'];](09-basic-ajax-post/backend.php) | Determine if a variable is declared and is different than null |\n\nWith **'application/x-www-form-urlencoded'** we need to use variables like:\n```\n//option 1\ntheObject.send('username=Maria');\n//option 2\ntheObject.send('username=' + encodeURIComponent(name));\n\n\u003c?php \nif (isset($_POST['username'])) {\n    echo \"Usuario: \" . $_POST['username'];\n}\n```\nIf you prefer, you can use **json**\n```\ntheObject.setRequestHeader('Content-Type', 'application/json');\ntheObject.send(JSON.stringify({ username: name }));\n\n\u003c?php \n$data = json_decode(file_get_contents(\"php://input\"), true);\nif (isset($data['username'])) {\n    echo \"Usuario: \" . $data['username'];\n} \n```\n\n\u003cbr/\u003e\n\n## Insert PHP from other file\nIn order to insert the code of another file we can use\n| Function  | Example | Explanation  |\n| ------------- | ------------- | ------------|\n| **include**  | [include 'functions.php';](04-importfiles/index.php) | The include expression includes and evaluates the specified file.|\n| **include_once**  | [include_once 'functions.php';](04-importfiles/index.php) | The include expression includes and evaluates the specified file only once |\n| **require**  | [require 'functions.php';](04-importfiles/index.php) | The require expression includes and evaluates the specified file. |\n| **require_once**  | [require_once 'functions.php';](04-importfiles/index.php) | The require expression includes and evaluates the specified file only once. |\n\n\n\u003e [!NOTE]\n\u003e The difference betweeen include and require is that the include construct will emit an E_WARNING if it cannot find a file; this is different behavior from require, which will emit an E_ERROR. So, the project of the includes will still be running although it doesn't find the file. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacamer%2Fphp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmacamer%2Fphp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacamer%2Fphp/lists"}