├── .gitignore ├── README.md ├── arrays ├── array_access.php ├── array_object.php ├── array_operations.php ├── count.php ├── example1.php ├── example2.php ├── example3.php ├── example4.php └── example5.php ├── basics ├── bitwise_operators.php ├── example1.php ├── example2.php ├── example3.php ├── numbers.php ├── strange_behaviours.php ├── tags.php └── variables.php ├── datatypes ├── json │ ├── decode.php │ ├── encode.php │ ├── error.json │ ├── error.php │ ├── error_encoding.json │ ├── error_encoding.php │ └── sample.json └── xml │ ├── error.xml │ ├── sample1.xml │ ├── sample2.xml │ ├── simplexml │ ├── creation.php │ ├── error_xml.php │ ├── error_xml_internal_errors.php │ ├── first_sample.php │ └── namespace_sample.php │ └── xmlReaderWriter │ ├── creation.php │ ├── error_xml.php │ ├── error_xml_internal_errors.php │ └── first_sample.php ├── functions ├── error.php ├── iscallable.php └── sample.php ├── objects └── visibilityChange.php ├── questions_pack ├── 1.php ├── 13.php ├── 14.php ├── 17.php ├── 2.php ├── 32.php ├── 4.php └── 50.php ├── strings └── basics.php └── test_questions ├── OOP ├── 10.php └── 10b.php ├── basics └── empty_and_constants.php ├── datatypes └── xml │ ├── question1.php │ ├── question1.xml │ ├── question1_corrected.php │ └── question1_corrected.xml ├── functions ├── 1.php ├── 2.php ├── 3.php ├── 4.php ├── 4b.php ├── 5.php └── 6.php └── objects ├── 11.php ├── 2.php └── 3.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-curiosities 2 | 3 | Aquí irán esos pequeños ejemplos de PHP que veamos en el club de formación de certificación PHP en Atrápalo. 4 | 5 | ## Superglobales: ## 6 | http://php.net/manual/en/language.variables.superglobals.php 7 | 8 | ## Error constants: ## 9 | http://php.net/manual/en/errorfunc.constants.php 10 | 11 | ## XPath: ## 12 | http://www.w3schools.com/xpath/ 13 | http://www.w3schools.com/xpath/xpath_syntax.asp 14 | 15 | ## Zend Certified PHP Engineer Exam Study Guide: ## 16 | https://www.dropbox.com/sh/vfptyn8kqzqvacs/AAABq6mbQQNFHDpBZXDYcsSCa/study/PHP_5-5_Study_Guide.pdf?dl=0 17 | 18 | ## Blog ZendCertificationDT: ## 19 | http://zendcertificationdt.netii.net/ 20 | 21 | ## Zend PHP 5 Certification Study Guide (Third Edition): ## 22 | https://www.dropbox.com/sh/vfptyn8kqzqvacs/AAAqxAZo7hnlvr0RX-JCxemoa/study/Zend%20PHP%205.5%20Certification%20Study%20Guide%2C%20Third%20Edition%2C%20A%20php%5Barchitect%5D%20Guide%20%28New%202015%29.pdf?dl=0 23 | -------------------------------------------------------------------------------- /arrays/array_access.php: -------------------------------------------------------------------------------- 1 | 1, 5 | 'name' => 'Jimmy', 6 | 'age' => 22, 7 | ]; 8 | 9 | $anObject = new \ArrayObject($anArray, ArrayObject::ARRAY_AS_PROPS); 10 | $anObject->name .= ' K. Oak'; 11 | $anObject->keyNueva = 'Pepito'; 12 | $a = $anObject['name']; 13 | 14 | var_dump($anObject, $a); 15 | 16 | echo "\nforeach: \n"; 17 | foreach ($anObject as $property => $value) { 18 | echo "$property => $value\n"; 19 | } -------------------------------------------------------------------------------- /arrays/array_operations.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimmyoak/php-curiosities/42efce1ba0e51e1b08bba23e2883b24c31cdfa4f/arrays/array_operations.php -------------------------------------------------------------------------------- /arrays/count.php: -------------------------------------------------------------------------------- 1 | 0, 7 | ' ' => 1, 8 | ' ' => 2, 9 | ' ' => 3, 10 | null => 4, 11 | false => 5 12 | ]; 13 | 14 | var_dump($anArray); -------------------------------------------------------------------------------- /arrays/example2.php: -------------------------------------------------------------------------------- 1 | name = 'asdasd'; 5 | $anArray = [ 6 | 0 => 0, 7 | 0.2 => 1, 8 | 0.999 => 2, 9 | '0' => 3, 10 | '0.4' => 4, 11 | '0hola' => 5, 12 | 0xA => 6, 13 | ]; 14 | 15 | //[0 => 3, '0.4' => 4, '0hola' => 5] 16 | 17 | var_dump($anArray); -------------------------------------------------------------------------------- /arrays/example3.php: -------------------------------------------------------------------------------- 1 | 0, 5 | '1' => 1, 6 | ]; 7 | 8 | var_dump($anArray); 9 | 10 | $anArray = [ 11 | '1' => 0, 12 | 1 => 1, 13 | ]; 14 | 15 | var_dump($anArray); -------------------------------------------------------------------------------- /arrays/example4.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'b' => 1, 7 | 'c' 8 | ] 9 | ]; 10 | 11 | echo $array[5][2]; 12 | echo $array[5][2] = 2; -------------------------------------------------------------------------------- /arrays/example5.php: -------------------------------------------------------------------------------- 1 | > 1;//Desplazamiento a derecha de los bits (1 posición) 16 | $shiftTwiceRight = $firstValue >> 2;//Desplazamiento a derecha de los bits (2 posiciones) 17 | 18 | echo 'AND:' . "\n"; 19 | echo decBinary($firstValue) . "\n"; 20 | echo decBinary($secondValue) . "\n"; 21 | echo '-------------------' ."\n"; 22 | echo decBinary($and). "\n\n\n"; 23 | 24 | echo 'OR:' . "\n"; 25 | echo decBinary($firstValue) . "\n"; 26 | echo decBinary($secondValue) . "\n"; 27 | echo '-------------------' ."\n"; 28 | echo decBinary($or). "\n\n\n"; 29 | 30 | echo 'XOR:' . "\n"; 31 | echo decBinary($firstValue) . "\n"; 32 | echo decBinary($secondValue) . "\n"; 33 | echo '-------------------' ."\n"; 34 | echo decBinary($xor). "\n\n\n"; 35 | 36 | echo 'NOT (~) [64 bits]:' . "\n"; 37 | echo decBinary($firstValue, 64) . "\n"; 38 | echo '-------------------' ."\n"; 39 | echo decBinary($not). "\n\n\n"; 40 | 41 | echo 'ZERO NOT (~) [64 bits]:' . "\n"; 42 | echo decBinary(0, 64) . "\n"; 43 | echo '-------------------' ."\n"; 44 | echo decBinary($zeroNot). "\n\n\n"; 45 | 46 | echo 'SHIFT LEFT (2^1) (x2):' . "\n"; 47 | echo decBinary($firstValue, 16) . "\n"; 48 | echo '-----------------------------' ."\n"; 49 | echo decBinary($shiftLeft, 16). "\n\n\n"; 50 | 51 | echo 'SHIFT TWICE LEFT (2^2) (x4):' . "\n"; 52 | echo decBinary($firstValue, 16) . "\n"; 53 | echo '-----------------------------' ."\n"; 54 | echo decBinary($shiftTwiceLeft, 16). "\n\n\n"; 55 | 56 | echo 'SHIFT RIGHT (2^1) (/2):' . "\n"; 57 | echo decBinary($firstValue, 16) . "\n"; 58 | echo '-----------------------------' ."\n"; 59 | echo decBinary($shiftRight, 16). "\n\n\n"; 60 | 61 | echo 'SHIFT TWICE RIGHT (2^2) (/4):' . "\n"; 62 | echo decBinary($firstValue, 16) . "\n"; 63 | echo '-----------------------------' ."\n"; 64 | echo decBinary($shiftTwiceRight, 16). "\n\n\n"; 65 | 66 | 67 | function decBinary($value, $positions = 8) 68 | { 69 | return sprintf('%5d', $value) . ': ' . binary($value, $positions); 70 | } 71 | function binary($value, $positions = 8) 72 | { 73 | return sprintf('%0'.$positions.'b', $value); 74 | } 75 | -------------------------------------------------------------------------------- /basics/example1.php: -------------------------------------------------------------------------------- 1 | 5 | 8 | 9 | 10 | 14 | 15 | <% 16 | //Only available if asp_tags is On in php.ini 17 | echo 'ASP TAGS'."\n"; 18 | %> 19 | 20 | 21 | -------------------------------------------------------------------------------- /basics/variables.php: -------------------------------------------------------------------------------- 1 | name = "Value"; 5 | $asClass->bigint = "34985776347563425873426587243562348756478"; 6 | 7 | //Simple class to json 8 | echo json_encode($asClass); 9 | 10 | echo "\n\n\n\n"; 11 | 12 | $asArray = [ 13 | 'name' => "Value", 14 | 'bigint' => '34985776347563425873426587243562348756478', 15 | 'properties' => [ 16 | 'a' => 1 17 | ], 18 | ]; 19 | 20 | //Simple array to json 21 | echo json_encode($asArray); 22 | 23 | echo "\n\n\n\n"; 24 | 25 | //Max depth error 26 | echo json_encode($asArray, null , 1); 27 | var_dump(json_last_error_msg()); 28 | 29 | echo "\n\n\n\n"; 30 | 31 | //With numbers 32 | echo json_encode($asArray, JSON_NUMERIC_CHECK); -------------------------------------------------------------------------------- /datatypes/json/error.json: -------------------------------------------------------------------------------- 1 | { 2 | 'name': "value" 3 | } -------------------------------------------------------------------------------- /datatypes/json/error.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /datatypes/xml/sample1.xml: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 0 5 | 1 6 | 1 7 | 1 8 | 0 9 | 10 | 11 | anImage.png 12 | anotherImage.png 13 | 14 | 16 |
-------------------------------------------------------------------------------- /datatypes/xml/sample2.xml: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 0 5 | 0 6 | 0 7 | 0 8 | 0 9 | 10 | 11 | anImage.png 12 | anotherImage.png 13 | aVideo.mp4 14 | 15 | 16 |
-------------------------------------------------------------------------------- /datatypes/xml/simplexml/creation.php: -------------------------------------------------------------------------------- 1 | '; 4 | 5 | $xml = new SimpleXMLElement($xmlRoot); 6 | 7 | $node = $xml->addChild('node'); 8 | $node->addAttribute('name', 'value'); 9 | 10 | $node->addChild('lead', 'Value'); 11 | 12 | echo $xml->asXML(); -------------------------------------------------------------------------------- /datatypes/xml/simplexml/error_xml.php: -------------------------------------------------------------------------------- 1 | data['length'] . ' /// type: ' . get_class($xml->data['length']) . "\n"; 12 | 13 | foreach ($xml->media->image as $image) { 14 | echo $image . ' /// type: ' . get_class($image) . "\n"; 15 | } 16 | 17 | $casted = (string)$image; 18 | var_dump($casted); -------------------------------------------------------------------------------- /datatypes/xml/simplexml/namespace_sample.php: -------------------------------------------------------------------------------- 1 | 9 | My Book 10 | 11 | Chapter 1 12 | Donec velit. Nullam eget tellus vitae tortor gravida scelerisque. 13 | In orci lorem, cursus imperdiet, ultricies non, hendrerit et, orci. 14 | Nulla facilisi. Nullam velit nisl, laoreet id, condimentum ut, 15 | ultricies id, mauris. 16 | 17 | 18 | Chapter 2 19 | Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin 20 | gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam 21 | vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros 22 | tellus, pharetra id, faucibus eu, dapibus dictum, odio. 23 | 24 | 25 | EOD; 26 | 27 | $sxe = new SimpleXMLElement($xml); 28 | 29 | $sxe->registerXPathNamespace('c', 'http://example.org/titulo-capitulo'); 30 | $resultado = $sxe->xpath('//c:titulo'); 31 | 32 | foreach ($resultado as $titulo) { 33 | echo $titulo . "\n"; 34 | } 35 | -------------------------------------------------------------------------------- /datatypes/xml/xmlReaderWriter/creation.php: -------------------------------------------------------------------------------- 1 | openMemory(); 5 | 6 | $xmlWriter->startDocument(1.0, 'UTF-8'); 7 | 8 | $xmlWriter->startElement('root'); 9 | $xmlWriter->startElement('node'); 10 | $xmlWriter->writeAttribute('length', 5); 11 | $xmlWriter->writeElement('leaf', 'Value'); 12 | $xmlWriter->endElement(); 13 | $xmlWriter->endElement(); 14 | 15 | echo $xmlWriter->outputMemory(); -------------------------------------------------------------------------------- /datatypes/xml/xmlReaderWriter/error_xml.php: -------------------------------------------------------------------------------- 1 | open($path); 7 | $xmlReader->read(); //Hasta que no hacemos el read no peta -------------------------------------------------------------------------------- /datatypes/xml/xmlReaderWriter/error_xml_internal_errors.php: -------------------------------------------------------------------------------- 1 | open($path); 9 | $xmlReader->read(); 10 | 11 | $errors = libxml_get_errors(); 12 | var_dump($errors); -------------------------------------------------------------------------------- /datatypes/xml/xmlReaderWriter/first_sample.php: -------------------------------------------------------------------------------- 1 | open($path); 7 | 8 | while($xmlReader->read()) { 9 | echo 'Name: ' . $xmlReader->name . " /// Type: " . $xmlReader->nodeType . " /// Value: " . $xmlReader->value; 10 | 11 | echo "\n"; 12 | } 13 | -------------------------------------------------------------------------------- /functions/error.php: -------------------------------------------------------------------------------- 1 | asdf(); -------------------------------------------------------------------------------- /questions_pack/1.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /questions_pack/14.php: -------------------------------------------------------------------------------- 1 | 10 && $one < 20) return $one; 6 | if($two > 10 && $two < 20) return $two; 7 | if($three > 10 && $three < 20) return $three; 8 | } 9 | 10 | $one = 2; 11 | $two = 20; 12 | $three = 15; 13 | 14 | $var = &find_variable($one, $two, $three); 15 | 16 | $var++; 17 | 18 | print "1: $one, 2: $two, 3: $three"; 19 | 20 | ?> -------------------------------------------------------------------------------- /questions_pack/17.php: -------------------------------------------------------------------------------- 1 | a = 10; 10 | } 11 | 12 | public function printValue() 13 | { 14 | print "The Value is: {$this->a}\n"; 15 | } 16 | 17 | public function changeValue($val, $obj = null) 18 | { 19 | if (is_null($obj)) { 20 | $this->a = $val; 21 | } else { 22 | $obj->a = $val; 23 | } 24 | } 25 | 26 | public function getValue() 27 | { 28 | return $this->a; 29 | } 30 | } 31 | 32 | 33 | 34 | $obj_one = new myClass(); 35 | $obj_two = new myClass(); 36 | 37 | $obj_one->changeValue(20, $obj_two); 38 | $obj_two->changeValue($obj_two->getValue(), $obj_one); 39 | 40 | $obj_two->printValue(); 41 | $obj_one->printValue(); 42 | 43 | ?> -------------------------------------------------------------------------------- /questions_pack/2.php: -------------------------------------------------------------------------------- 1 | m(); -------------------------------------------------------------------------------- /questions_pack/32.php: -------------------------------------------------------------------------------- 1 | something(); 29 | } catch (AnotherException $e) { 30 | $a->somethingElse(); 31 | } catch (MyException $e) { 32 | print "Caught Exception"; 33 | } 34 | } catch (Exception $e) { 35 | print "Didn't catch the Exception!"; 36 | } 37 | 38 | //Caso curioso: 39 | /* 40 | try { 41 | throw new \Exception(); 42 | } catch (\OdinException $e) { 43 | 44 | } catch (\Exception $e) { 45 | 46 | } 47 | */ -------------------------------------------------------------------------------- /questions_pack/4.php: -------------------------------------------------------------------------------- 1 | 0, 2, 3, 4]; 4 | 5 | $count = count($array); 6 | 7 | $slice = array_slice($array, 3); 8 | 9 | $merge = array_merge(['x'], $slice); 10 | 11 | array_splice($array, 3, $count, $merge); 12 | 13 | var_dump($count, $slice, $merge); 14 | print_r($array); 15 | 16 | $array = ['a' => 2, 'b' => 3, 'c' => 4]; 17 | var_dump($array); 18 | array_splice($array, 1, 1); 19 | var_dump($array); 20 | -------------------------------------------------------------------------------- /strings/basics.php: -------------------------------------------------------------------------------- 1 | 26 | ----- 27 | 28 | NOWDOC; 29 | 30 | $jugos = ["manzana", "naranja", "koolaid1" => "p˙rpura"]; 31 | echo "…l tomÛ algo de jugo $jugos[koolaid1]." . PHP_EOL; 32 | 33 | $foo = ["bar" => "naranja"]; 34 | echo "esto est· bien: " . $foo['bar'] . PHP_EOL; 35 | echo "esto est· bien: $foo[bar]" . PHP_EOL; 36 | // Esto no funciona por la misma razÛn que $foo[bar] es incorrecto fuera de un string. 37 | //echo "esto est· mal " . $foo[bar]; 38 | 39 | 40 | /* SUBSTRING */ 41 | echo "----------" .PHP_EOL; 42 | echo substr('abcdef', 1) . PHP_EOL; // bcdef 43 | echo substr('abcdef', 1, 3) . PHP_EOL; // bcd 44 | echo substr('abcdef', 0, 4) . PHP_EOL; // abcd 45 | echo substr('abcdef', 0, 8) . PHP_EOL; // abcdef 46 | echo substr('abcdef', -1, 1) . PHP_EOL; // f 47 | echo var_dump(substr('a', 1)) . PHP_EOL; //bool(false) 48 | 49 | 50 | -------------------------------------------------------------------------------- /test_questions/OOP/10.php: -------------------------------------------------------------------------------- 1 | "A", "b" => "B", "c" => "C"]; 9 | protected $c = [1, 2, 3]; 10 | 11 | public function __get($v) 12 | { 13 | echo "$v"; 14 | return $this->b[$v]; 15 | } 16 | 17 | public function __set($var, $val) 18 | { 19 | echo "$var:$val,"; 20 | $this->$var = $val; 21 | } 22 | } 23 | $m = new Magic(); 24 | echo $m->a.",".$m->b.",".$m->c.","; 25 | $m->c = "CC"; 26 | echo $m->a.",".$m->b.",".$m->c; 27 | 28 | 29 | //bcA,B,C,c:CC,bcA,B,C -------------------------------------------------------------------------------- /test_questions/OOP/10b.php: -------------------------------------------------------------------------------- 1 | "A", "b" => "B", "c" => "C"]; 9 | protected $c = [1, 2, 3]; 10 | 11 | public function __get($v) 12 | { 13 | echo "$v,"; 14 | return $this->b[$v]; 15 | } 16 | 17 | public function __set($var, $val) 18 | { 19 | echo "$var:$val,"; 20 | $this->$var = $val; 21 | } 22 | } 23 | $m = new Magic(); 24 | echo $m->a.",".$m->b.",".$m->c.","; 25 | $m->c = "CC"; 26 | echo $m->a.",".$m->b.",".$m->c; 27 | 28 | 29 | //b,c,A,B,C,c:CC,b,c,A,B,C -------------------------------------------------------------------------------- /test_questions/basics/empty_and_constants.php: -------------------------------------------------------------------------------- 1 | leaf; -------------------------------------------------------------------------------- /test_questions/datatypes/xml/question1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Value 5 | 6 | 7 | 8 | 9 | Value 10 | -------------------------------------------------------------------------------- /test_questions/datatypes/xml/question1_corrected.php: -------------------------------------------------------------------------------- 1 | leaf; -------------------------------------------------------------------------------- /test_questions/datatypes/xml/question1_corrected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Value 5 | -------------------------------------------------------------------------------- /test_questions/functions/1.php: -------------------------------------------------------------------------------- 1 | 'A', 'b' => 'B', 'c' => 'C']; 6 | protected $c = [1,2,3]; 7 | 8 | public function __get($v) 9 | { 10 | echo "$v,"; 11 | return $this->b[$v]; 12 | } 13 | 14 | public function __set($var, $val) 15 | { 16 | echo "$var: $val,"; 17 | $this->$var = $val; 18 | } 19 | } 20 | 21 | $m = new Magic(); 22 | echo $m->a.','.$m->b.','.$m->c.','; 23 | $m->c = 'CC'; 24 | echo $m->a.','.$m->b.','.$m->c; -------------------------------------------------------------------------------- /test_questions/objects/2.php: -------------------------------------------------------------------------------- 1 | a(); 26 | 27 | //It works on 5.5, Fuck you 5.3 -------------------------------------------------------------------------------- /test_questions/objects/3.php: -------------------------------------------------------------------------------- 1 | threeDots(); 13 | } 14 | } 15 | 16 | $a = new myClassA(); 17 | $a->doSomething(); --------------------------------------------------------------------------------