php

print array

SOFTWARE: php

print_r($array);

print contents of array

MORE INFO:

alternate if else statement

SOFTWARE: php

$myvar = empty($value1) ? '' : 'something';

Called the "ternary"operator. An alternate way to create if else statements in PHP. In the example, we're saying: "If $value1 is empty, then make $myvar nothing (''), otherwise, make it 'something'. In other words, the question mark is like "if then" and the colon is like "else" ... do this.

MORE INFO:

php forms

SOFTWARE: php

This is just a scratchy personal note: NOT to be used for general reference! Getting my noggin around the idea of using predefined variables in PHP for forms construction.

Set request variables to empty if empty, value of request if set

$variable = empty($_REQUEST['variable']) ? '' : $_REQUEST['variable'];

Submit a form to itself

Interesting note that bluecloth (markdown) won't allow nested tags in code sequences yet. Supposed to be fixed in version 1.0.1 (pending).

<form method="post" action="(start php tag here)=$_SERVER['PHP_SELF'](end php tag here)">
MORE INFO:

additional params for mail

SOFTWARE: php

mail($to, $subject, $message, $headers, $additional_parameters);

When using the mail function to send email with php, there is a fifth parameter, "additional", that you can specify which allows you to force things like "Return-Path" into the headers. Useful if you can't get all the header info you want in with the standard fourth, "additional headers" param. For example you can do something like this to force "Return-Path":

mail('user@somesite.com', 'subject', "hello', null, '-f me@mysite.com');

The "-f" flag represents options to sendmail. You can send other options as well. Note that the basic structure of the params is:

mail($to, $subject, $message, $headers, $additional_parameters);

So most stuff you can get into $headers (like 'From: me@mysite' etc.) but a few others seem to be only accessible through the additional headers param. Return-Path is especially useful to avoid getting tagged as spam due to your server not being recognized by the recipient's system.

More info on php.net

MORE INFO:

pretty date time format

SOFTWARE: php

$ptime = date("l M jS - g\:i a", $unixtime);

The date() function in php lets you designate how to make dates and times display. It takes two basic things: format, unix timestamp. (List of all format options on php.net via the reference URL for this cheat.)

$p_time = date("l M jS - g\:i a", $unix_time);

Note that it expects unix format timestamp. So if you have your date in standard 00-00-0000 00:00:00 format instead, you can transform that first to unix format timestamp with strtotime() function.

//get me a time from the db:
$raw_time = $row['event_start'];

//transform it to unix timestamp:
$unix_time = strtotime($raw_time);

//now format it to display the way I want:
$p_time = date("l M jS - g\:i a", $unix_time);

Another approach is to make your select statement grab the date time out of the db pre-formatted just how you like. See:

mysql manual

MORE INFO: http://www.php.net/manual/en/function.date.php
Comments