PPPHP

Global Helper Functions

These functions are available everywhere in your pp-php project – in controllers, views, and custom code. No class prefix is needed; just call them directly. They are auto‑loaded from core/helpers.php.

Contents

Date & Time

now()

Returns the current datetime in Y-m-d H:i:s format.

echo now();  // 2025-06-15 14:32:05

// Use with database inserts
DB::Insert('orders', [
    'customer' => 'John',
    'created_at' => now()
]);

today()

Returns the current date only (Y-m-d).

echo today();  // 2025-06-15

// Select orders placed today
$rs = DB::Select('orders', 'DATE(created_at) = ' . DB::quote(today()));

timeAgo()

Converts a datetime into a human‑readable “time ago” string.

echo timeAgo('2025-06-15 14:20:00');  // "5 minutes ago"
echo timeAgo('2025-06-10 09:00:00');  // "5 days ago"
echo timeAgo('2024-12-01 00:00:00');  // "6 months ago"

// Use in a view to show when a record was last updated
echo 'Last updated ' . timeAgo($record['updated_at']);

formatDate()

Formats a datetime into any custom pattern.

echo formatDate('2025-06-15 14:32:05');               // "Jun 15, 2025, 2:32 pm"
echo formatDate('2025-06-15', 'l, F j, Y');            // "Sunday, June 15, 2025"
echo formatDate($row['created_at'], 'd/m/Y H:i');       // "15/06/2025 14:32"

String Helpers

slug()

Creates a URL‑safe slug from any text.

echo slug('Hello World!');      // "hello-world"
echo slug('Product #123 ABC');   // "product-123-abc"
echo slug('  Multiple   Spaces  ');  // "multiple-spaces"

// Auto‑generate slugs for project names
$slug = slug($_POST['project_name']);

truncate()

Cuts a string to a maximum length and appends an ellipsis.

echo truncate('This is a very long description that needs to be shortened.', 20);
// "This is a very long…"

echo truncate('Short text', 100);  // "Short text" (unchanged)

// Custom ellipsis
echo truncate('Long text here', 10, '…read more');
// "Long text…read more"

nl2brSafe()

Converts newlines to <br> tags, with HTML escaping for security.

echo nl2brSafe("Line 1\nLine 2\nLine 3");
// Line 1
Line 2
Line 3 // Safe against XSS echo nl2brSafe("<script>alert('xss')</script>\nNew line"); // &lt;script&gt;alert('xss')&lt;/script&gt;
New line

plain()

Strips all HTML tags, optionally truncates the result.

echo plain('<b>Bold text</b> and <i>italic</i>');
// "Bold text and italic"

echo plain('<p>A long paragraph</p>', 10);
// "A long par…"

Array Helpers

first()

Returns the first element of an array, or null if empty.

$users = DB::Select('users');
$firstUser = first($users);
echo $firstUser['name'];  // "Alice"

last()

Returns the last element of an array, or null if empty.

$logs = DB::Select('activity_log');
$lastEntry = last($logs);
echo $lastEntry['event'];  // "User logged out"

URL & Redirect

redirect()

Sends an HTTP redirect and stops execution immediately.

// Redirect to the login page
redirect('/login');

// Redirect to an external URL
redirect('https://example.com/dashboard');

// After a successful form submission
redirect('/projects?created=1');

get() / post()

Safely retrieve values from $_GET or $_POST with a default fallback.

$page = get('page', 1);          // current page number, defaults to 1
$search = get('q', '');          // search term, defaults to empty string
$sort = get('sort', 'name ASC'); // sorting, with a default

$email = post('email', '');      // from a form
$remember = post('remember', false);

// Combine with validation
$id = (int) get('id', 0);
if ($id <= 0) {
    redirect('/error?msg=invalid_id');
}

Encryption & Hashing

encrypt() / decrypt()

Encrypt/decrypt strings using AES‑256‑CBC. Useful for storing sensitive data in the database.

$key = 'your-32-byte-secret-key-here!!';  // keep this safe!

// Encrypt a credit card number before storage
$encrypted = encrypt('4111-1111-1111-1111', $key);
DB::Insert('payments', ['card_hash' => $encrypted]);

// Decrypt for display
$card = decrypt($encrypted, $key);
echo 'Card ending in ' . substr($card, -4);   // "Card ending in 1111"

Validation

isEmail()

Checks if a string is a valid email address.

if (!isEmail(post('email'))) {
    echo 'Please enter a valid email address.';
}

// Validate from a form
$email = post('email', '');
if (isEmail($email)) {
    DB::Insert('subscribers', ['email' => $email]);
}

isUrl()

Checks if a string is a valid URL.

if (!isUrl(post('website'))) {
    echo 'Please enter a valid URL starting with http:// or https://';
}

// Use in a loop
foreach ($links as $url) {
    if (isUrl($url)) {
        echo '<a href="' . $url . '">Valid link</a>';
    }
}

isJson()

Checks if a string is valid JSON.

$input = '{"name": "John", "age": 30}';
if (isJson($input)) {
    $data = json_decode($input, true);
    echo $data['name'];  // "John"
} else {
    echo 'Invalid JSON format.';
}

🔗 Related Help Topics

Database API Security API