1 | <?php |
||
2 | /* |
||
3 | * YOURLS |
||
4 | * Compatibility functions when either missing from older PHP versions or not included by default |
||
5 | */ |
||
6 | |||
7 | // @codeCoverageIgnoreStart |
||
8 | |||
9 | /** |
||
10 | * json_encode for PHP, should someone run a distro without php-json -- see http://askubuntu.com/questions/361424/ |
||
11 | * |
||
12 | */ |
||
13 | if( !function_exists( 'json_encode' ) ) { |
||
14 | function json_encode( $array ) { |
||
15 | return yourls_array_to_json( $array ); |
||
16 | } |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds. |
||
21 | * |
||
22 | * @param array $array the array to convert. |
||
23 | * @return mixed The resulting JSON string, or false if the argument was not an array. |
||
24 | * @author Andy Rusterholz |
||
25 | * @link http://php.net/json_encode (see comments) |
||
26 | */ |
||
27 | function yourls_array_to_json( $array ){ |
||
28 | |||
29 | if( !is_array( $array ) ){ |
||
30 | return false; |
||
31 | } |
||
32 | |||
33 | $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) )); |
||
34 | if( $associative ){ |
||
35 | |||
36 | $construct = array(); |
||
37 | foreach( $array as $key => $value ){ |
||
38 | |||
39 | // We first copy each key/value pair into a staging array, |
||
40 | // formatting each key and value properly as we go. |
||
41 | |||
42 | // Format the key: |
||
43 | if( is_numeric( $key ) ){ |
||
44 | $key = "key_$key"; |
||
45 | } |
||
46 | $key = '"'.addslashes( $key ).'"'; |
||
47 | |||
48 | // Format the value: |
||
49 | if( is_array( $value )){ |
||
50 | $value = yourls_array_to_json( $value ); |
||
51 | } else if( !is_numeric( $value ) || is_string( $value ) ){ |
||
52 | $value = '"'.addslashes( $value ).'"'; |
||
53 | } |
||
54 | |||
55 | // Add to staging array: |
||
56 | $construct[] = "$key: $value"; |
||
57 | } |
||
58 | |||
59 | // Then we collapse the staging array into the JSON form: |
||
60 | $result = "{ " . implode( ", ", $construct ) . " }"; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
, does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation.
Loading history...
Coding Style
Comprehensibility
introduced
by
The string literal
} does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation.
Loading history...
|
|||
61 | |||
62 | } else { // If the array is a vector (not associative): |
||
63 | |||
64 | $construct = array(); |
||
65 | foreach( $array as $value ){ |
||
66 | |||
67 | // Format the value: |
||
68 | if( is_array( $value )){ |
||
69 | $value = yourls_array_to_json( $value ); |
||
70 | } else if( !is_numeric( $value ) || is_string( $value ) ){ |
||
71 | $value = '"'.addslashes($value).'"'; |
||
72 | } |
||
73 | |||
74 | // Add to staging array: |
||
75 | $construct[] = $value; |
||
76 | } |
||
77 | |||
78 | // Then we collapse the staging array into the JSON form: |
||
79 | $result = "[ " . implode( ", ", $construct ) . " ]"; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
[ does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation.
Loading history...
Coding Style
Comprehensibility
introduced
by
The string literal
, does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation.
Loading history...
Coding Style
Comprehensibility
introduced
by
The string literal
] does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation.
Loading history...
|
|||
80 | } |
||
81 | |||
82 | return $result; |
||
83 | } |
||
84 | |||
85 | |||
86 | /** |
||
87 | * BC Math functions (assuming if one doesn't exist, none does) |
||
88 | * |
||
89 | */ |
||
90 | if ( !function_exists( 'bcdiv' ) ) { |
||
91 | function bcdiv( $dividend, $divisor ) { |
||
92 | $quotient = floor( $dividend/$divisor ); |
||
93 | return $quotient; |
||
94 | } |
||
95 | function bcmod( $dividend, $modulo ) { |
||
96 | $remainder = $dividend%$modulo; |
||
97 | return $remainder; |
||
98 | } |
||
99 | function bcmul( $left, $right ) { |
||
100 | return $left * $right; |
||
101 | } |
||
102 | function bcadd( $left, $right ) { |
||
103 | return $left + $right; |
||
104 | } |
||
105 | function bcpow( $base, $power ) { |
||
106 | return pow( $base, $power ); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // @codeCoverageIgnoreEnd |
||
111 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.