Welcome to the Cookbook

loading...

3.15.1 Global Functions

Here are CakePHP's globally available functions. Many of them are convenience wrappers for long-named PHP functions, but some of them (like uses()) can be used to include code or perform other useful functions. Chances are if you're constantly wanting a function to accomplish an oft-used task, it's here.

3.15.1.1 __

__(string $string_id, boolean $return = false)

This function handles localization in CakePHP applications. The $string_id identifies the ID for a translation, and the second parameter allows you to have the function automatically echo the string (the default behavior), or return it for further processing (pass a boolean true to enable this behavior).

Check out the Localization & Internationalization section for more information.

3.15.1.2 a

a(mixed $one, $two, $three...)

Returns an array of the parameters used to call the wrapping function.

print_r(a('foo', 'bar')); 

// output:
array(
   [0] => 'foo',
   [1] => 'bar'
)
  1. print_r(a('foo', 'bar'));
  2. // output:
  3. array(
  4. [0] => 'foo',
  5. [1] => 'bar'
  6. )

3.15.1.3 aa

aa(string $one, $two, $three...)

Used to create associative arrays formed from the parameters used to call the wrapping function.

print_r(aa('a','b')); 

// output:
array(
    'a' => 'b'
)
  1. print_r(aa('a','b'));
  2. // output:
  3. array(
  4. 'a' => 'b'
  5. )

3.15.1.4 am

am(array $one, $two, $three...)

Merges all the arrays passed as parameters and returns the merged array.

3.15.1.5 config

Can be used to load files from your application config-folder via include_once. Function checks for existance before include and returns boolean. Takes an optional number of arguments.

Example: config('some_file', 'myconfig');

3.15.1.6 convertSlash

convertSlash(string $string)

Converts forward slashes to underscores and removes the first and last underscores in a string. Returns the converted string.

3.15.1.7 countdim

countdim(array $array)

Returns the number of dimensions in the supplied array.

3.15.1.8 debug

debug(mixed $var, boolean $showHtml = false)

If the application's DEBUG level is non-zero, $var is printed out. If $showHTML is true, the data is rendered to be browser-friendly.

3.15.1.9 e

e(mixed $data)

Convenience wrapper for echo().

3.15.1.10 env

env(string $key)

Gets an environment variable from available sources. Used as a backup if $_SERVER or $_ENV are disabled.

This function also emulates PHP_SELF and DOCUMENT_ROOT on unsupporting servers. In fact, it's a good idea to always use env() instead of $_SERVER or getenv() (especially if you plan to distribute the code), since it's a full emulation wrapper.

3.15.1.11 fileExistsInPath

fileExistsInPath(string $file)

Checks to make sure that the supplied file is within the current PHP include_path. Returns a boolean result.

3.15.1.12 h

h(string $text, string $charset = null)

Convenience wrapper for htmlspecialchars().

3.15.1.13 ife

ife($condition, $ifNotEmpty, $ifEmpty)

Used for ternary-like operations. If the $condition is non-empty, $ifNotEmpty is returned, else $ifEmpty is returned.

3.15.1.14 low

low(string $string)

Convenience wrapper for strtolower().

3.15.1.15 paths

paths()

Get CakePHP basic paths as an indexed array. Resulting array will contain array of paths indexed by: Models, Behaviors, Controllers, Components, and Helpers.

This has been Deprecated and is no longer available in RC2. Use Configure::corePaths(); instead.

3.15.1.16 pr

pr(mixed $var)

Convenience wrapper for print_r(), with the addition of wrapping <pre> tags around the output.

3.15.1.17 r

r(string $search, string $replace, string $subject)

Convenience wrapper for str_replace().

3.15.1.18 stripslashes_deep

stripslashes_deep(array $value)

Recursively strips slashes from the supplied $value. Returns the modified array.

3.15.1.19 up

up(string $string)

Convenience wrapper for strtoupper().

3.15.1.20 uses

uses(string $lib1, $lib2, $lib3...)

Used to load CakePHP's core libraries (found in cake/libs/). Supply the name of the library's file name without the '.php' extension.