PHP 测试插入类?

<?php

class dbControls {
    var $title   = '';
    var $content = '';
    var $date    = '';

    function insertArticle()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
    }
}
?>

PHP 用户PHP的heredoc语法

<?php
foreach ($contacts as $contact_object) {
  $contact_array = (array) $contact_object;
  foreach ($contact_array as $key=>$value) $$key = h($value);
  echo <<<END_ROW
    <tr>
      <td>$id</td>
      <td>$first_name</td>
      <td>$last_name</td>
      <td>$phone</td>
      <td>
        <a href="?action=edit&id=$id">edit</a>
        <a href="?action=delete&id=$id">delete</a>
      </td>
    </tr>
END_ROW;
}

PHP diferencia de dias

$diferencia_dias = (strtotime($fecha1) - strtotime($fecha2)) / (60 * 60 * 24);

PHP 简单的PHP导航

//Place this in an include
<ul>
	<li><a href="../index.php" <?php if($thisPage == 'home') echo 'id="here"';?> >Home</a></li>
	<li><a href="../page1/index.php" <?php if($thisPage == 'page1') echo 'id="here"';?> >Page 1</a></li>
	<li><a href="../page2/index.php" <?php if($thisPage == 'page2') echo 'id="here"';?> >Page 2</a></li>
	<li><a href="../page3/index.php" <?php if($thisPage == 'page3') echo 'id="here"';?> >Page 3</a></li>
</ul>

//This php goes inside each page, notice the variable
<?php 
	$thisPage = 'home';
	include('includes/navigation.inc.php');
?>

PHP file_put_contents

define('FILE_APPEND', 1);

function file_put_contents($filename, $data, $flag = false) {
    $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a+' : 'w+';
    $fp = fopen($filename, $mode);
    if ($fp === false) {
        return 0;
    } else {
        if (is_array($data)) $data = implode($data);
        $bytes_written = fwrite($fp, $data);
        fclose($fp);
        return $bytes_written;
    }
}

PHP PHP - Ceros a la izquierda

function ceros($numero, $ceros=2){
    return sprintf("%0".$ceros."s", $numero );
}

echo ceros(2, 5); #out 000002

PHP GD&GET vars的图像缩略图

<?php
require_once "Image/Transform/Driver/GD.php";

if(isset($_GET['p']) && isset($_GET['s'])) {
	$path = "thumbs/".$_GET['p'];
	$sideSize = (int)$_GET['s'];
		
	$image = new Image_Transform_Driver_GD();
	$image->load($path);
	
	if($image->getImageWidth() > $image->getImageHeight()) {
		$image->scaleByY($sideSize);
		$result = $image->crop($sideSize, $sideSize, ($image->new_x - $sideSize)/2, 0);
	} else {
		$image->scaleByX($sideSize);
		$image->crop($sideSize, $sideSize, 0, ($image->new_y - $sideSize)/2);
	}
	
	$image->display();
} else {
	trigger_error("Bad GET vars");
}
?>

PHP 相关参赛作品转发

global $DB;

$rel_weblog_id = '4';
$rel_field_value = '{rel_field}';

$sql = "
	SELECT *
	FROM exp_relationships
	LEFT JOIN exp_weblog_data, exp_weblog_titles
	ON (
		exp_weblog_data.entry_id = exp_relationships.rel_child_id
		AND exp_weblog_titles.entry_id = exp_relationships.rel_child_id
		)
	WHERE exp_relationships.rel_id = '$rel_field_value'
	AND exp_weblog_titles.weblog_id = '$rel_weblog_id'
";

// Query for rel_child_id in the exp_relationships table
$rel_data_query = $DB->query($sql);

if($rel_data_query->num_rows > 0)
{
	foreach($rel_entry_query->result as $row)
	{
		$entry_id = $row['entry_id'];
		$title = $row['title'];
	}
}

PHP LimitIterator

$items = array(
    'apple',
    'lemon',
    'orange',
    'grape',
    'melon',
    'watermelon',
    'strawberry',
    'banana',
    'mango',
    'lime'
);

$offset = 3;

$count = 5;


$innerIterator = new ArrayIterator($items);

$itarator = new LimitIterator($innerIterator, $offset, $count);


foreach ($iterator as $current) {
    print $current;
}

PHP Obtenir idioma del navegador

function getIdiomaNavegador ()
	{
		if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
		{
			$idiomes = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
			for($i = 0;$i < count($idiomes);$i++)
			{				
				if (strlen($idiomes[$i]) > 2) $idiomes[$i] = substr($idiomes[$i], 0, 2);
				if (in_array($idiomes[$i], $this->IDIOMES_COMPARTIR))
				{
					$idioma = array_keys($this->IDIOMES_COMPARTIR, $idiomes[$i]);
					return ($idioma[0] + 1);
				}
			}
		}
		return 1;