> For the complete documentation index, see [llms.txt](https://openai.gitbook.io/code-cheatsheets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openai.gitbook.io/code-cheatsheets/all/php.md).

# PHP

## Running

See setup and AMP notes, need the server

## Basics

```php
<?php
  //code here
  $my_name = "JJ";
  print($my_name);
    print "Hi"
?>
<div> body </div>
```

* dynamically typed language
* All **variable** names begin with a `$` followed by letters digits or underscores

### Printing

* echo //no return type
* print //return true always
* if you use (), only one argument
* should do `$s." ".$t`

### Functions

* Function names are not case sensitive
* & in function def is pass by reference
* javascript scoping for functions not {}
* global declaration `global $x` makes it Avaliable everywhere

  ```php
  <?php
  function increment(&$num) {
  // this $num is the actual parameter below
  $num++;
    print('$num in function: ' . "$num");
  }
  ?>
  ```

### Strings

```php
<?php 
  "Passwd:$password[Joe]" #double quotes allow interpolation
  $name = 'j' . 'f' #concatination
  $str = implode("^",$ar") #join in python
  $ar = explode(":",$str); #split in python
?>
```

### Arrays

Basically dictionaries that if not set use numeric indexes

```php
<?php
$ar = array("key1"=>"value 1", 67=>3, "a"=>10, 4=>"Hi");
$x = array();
$my_array[2] = 2006; // this is literally declaring an array
$my_array = array("Hello",2,TRUE);

$usernames = array_keys($ar);
$passwds= array_values($ar);
?>
```

*Functions to call on arrays*

* unset //entry or entire array
* count OR sizeof
* is\_array

### Control

* if and then like C++, switch statement

  **Loops**
* while, for

```php
<?php
  foreach($array as $value) {}
  foreach($array as $key => $value){}
  for ($i = 0; $i < 10; $i++) {
    //
  }
?>
```

### Big Project Functions

* isset($x)
* gettype($x)

## Includes

`include("filename")`

* check

## ERRORS

Do not put a space between #!/usr/local/bin/php \<?php ?>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openai.gitbook.io/code-cheatsheets/all/php.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
