---
# System prepended metadata

title: PHP code examples & snippets
tags: [Development]

---

# PHP code examples & snippets

###### tags: `Development`

```
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
     
     
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
```


```
$stmt = $pdo->query('SELECT * FROM meta WHERE imported = 0 LIMIT 0,4000');
	while ($row = $stmt->fetch())
	{
    
    }
	$stmt->closeCursor();
```

```
header("Access-Control-Allow-Origin: *");

function utf8ize($d) {
    if (is_array($d)) 
        foreach ($d as $k => $v) 
            $d[$k] = utf8ize($v);

     else if(is_object($d))
        foreach ($d as $k => $v) 
            $d->$k = utf8ize($v);

     else 
        return utf8_encode($d);

    return $d;
}


header('Content-Type: application/json; charset=utf-8');

echo( json_encode(utf8ize($response)) );




function startsWith( $haystack, $needle ) {
     $length = strlen( $needle );
     return substr( $haystack, 0, $length ) === $needle;
}

function endsWith( $haystack, $needle ) {
    $length = strlen( $needle );
    if( !$length ) {
        return true;
    }
    return substr( $haystack, -$length ) === $needle;
}


```