background preloader

PHP

Facebook Twitter

Drupal

PHP Variables passed by value or by reference. Extending Exceptions. <? Php class MyException extends Exception { /* Redefine a exceção para que a mensagem não seja opcional */ public function __construct($message, $code = 0) { // coisas personalizadas que você queira fazer // ... /* Garante que tudo é atribuído corretamente */ parent::__construct($message, $code); } public function customFunction() { echo "Uma função personalizada para esse tipo de exceção\n"; } /** * Cria uma classe para testar a exceção */class TestException { public $var; const THROW_NONE = 0; const THROW_CUSTOM = 1; const THROW_DEFAULT = 2; function __construct($avalue = self::THROW_NONE) { switch ($avalue) { case self::THROW_CUSTOM: // dispara exceção personalizada throw new MyException('1 é um parâmetro inválido', 5); break; case self::THROW_DEFAULT: // dispara a padrão throw new Exception('2 não é permitido como parâmetro', 6); break; default: // Nenhuma exceção, objeto será criado.

Juan Treminio - Dallas PHP/MySQL Web Developer — Composer Namespaces in 5 Minutes. Install MySQL Server 5 on Ubuntu. PHP string to int. This small tutorial shows you how to convert PHP string to int value. Tutorial info: Bookmark PHP string to int Step 1 - Convert PHP string to int value PHP string to int Sometimes it is important to have the value of a variable in int format. To convert a PHP string to int is quite easy. Code: To check if the code realy works we can use the === operator. One more question is open. "10" -> 10"10.5" -> 10"10,5" -> 10"10 " -> 10" 10 " -> 10"10test" -> 10"test10" -> 0 Tags: php string to int, string to int, string int. Logging With PHP. I have to warn you not to be misled by the title of this article. It isn't really about cutting down trees with PHP. I know that PHP is the Swiss Army knife of Web scripting languages, but I don't think its developers have come up with a way to have it reduce our tree cover (at least not yet).

Rather, this article is about a different sort of logging - the sort which involves writing messages to files or devices on a regular basis, and using these messages to "do something useful", be it generating a report or building an audit trail for activity tracking. Over the next few pages, I'm going to take a quick look at the various mechanisms available to log script activity via PHP, demonstrating both built-in functions and add-on classes that allow you to add logging functionality to your applications. Using PHP_SELF in the action field of a form. In this article shows the usage of PHP_SELF variable and how to avoid PHP_SELF exploits. What is PHP_SELF variable? PHP_SELF is a variable that returns the current script being executed. This variable returns the name and path of the current file (from the root folder).

You can use this variable in the action field of the FORM. There are also certain exploits that you need to be aware of. We shall discuss all these points in this article. A) Suppose your php file is located at the address: In this case, PHP_SELF will contain:"/form-action.php" b) Suppose your php file is located at the address: For this URL, PHP_SELF will be :"/dir1/form-action.php" Using the PHP_SELF variable in the action field of the form A common use of PHP_SELF variable is in the action field of the <form> tag.

Consider, you have a file called form-action.php and want to load the same page after the form is submitted.

Symfony

Doctrine. How to send correct UTF-8 mail in PHP | bitPrison.net. Optimal bcrypt work factor. Simple PHP 5.3+ Bcrypt class and functions. Get json using php. Json_decode. My initial problem was to have PHP check a form in case JavaScript was disabled on the client. I fiddled with json_decode for a while before realizing what I really wanted: to be able to initialize the same object in PHP and JavaScript from a common source file.

I ended up writing a tiny parser for a JavaScript object initializer, which is close to - but not the same thing as - a piece of JSON. Among other things, it - recognizes regexes (turning them into PHP strings), - handles C/C++ comments - accepts non-quoted field names. This parser will accept a superset of real JS object initializer syntax (for instance non-quoted string litterals or improperly formed regexes). Here is the code for those who are interrested : define ('JSVAL_TEXT' , 12001); define ('JSVAL_STRING', 12002); define ('JSVAL_REGEXP', 12003); define ('JSVAL_COMMT1', 12004); define ('JSVAL_COMMT2', 12005); Handling JSON like a boss in PHP. There are already lots of tutorials out there on handling JSON with PHP, but most of them don’t go much deeper than throwing an array against json_encode and hoping for the best.

This article aims to be a solid introduction into JSON and how to handle it correctly in combination with PHP. Also, readers who don’t use PHP as their programming language can benefit from the first part that acts as a general overview on JSON. JSON (JavaScript Object Notation) is a data exchange format that is both lightweight and human-readable (like XML, but without the bunch of markup around your actual payload). Its syntax is a subset of the JavaScript language that was standardized in 1999. The cool thing about JSON is that you can handle it natively in JavaScript, so it acts as the perfect glue between server- and client-side application logic.

Before we start handling JSON in PHP, we need to take a short look at the JSON specification to understand how it is implemented and what’s possible. Summary. Handling data in a PHP JSON Object. Klassen und Objekte. Maybe someone will find these classes, which simulate enumeration, useful. <? Phpclass Enum { protected $self = array(); public function __construct( ) { $args = func_get_args(); for( $i=0, $n=count($args); $i<$n; $i++ ) $this->add($args[$i]); } public function __get( $name = null ) { return $this->self[$name]; } public function add( $name = null, $enum = null ) { if( isset($enum) ) $this->self[$name] = $enum; else $this->self[$name] = end($this->self) + 1; } } class DefinedEnum extends Enum { public function __construct( $itms ) { foreach( $itms as $name => $enum ) $this->add($name, $enum); } } class FlagsEnum extends Enum { public function __construct( ) { $args = func_get_args(); for( $i=0, $n=count($args), $f=0x1; $i<$n; $i++, $f *= 0x2 ) $this->add($args[$i], $f); } }?

>Example usage:<? Php $eFruits = new Enum("APPLE", "ORANGE", "PEACH"); echo $eFruits->APPLE . ","; echo $eFruits->ORANGE . ","; echo $eFruits->PEACH . "\n"; File Read. My apologies for taking so long to actually get to the point where you get information from files. In this lesson we will teach you how to read data from a file using various PHP functions. Before we can read information from a file we have to use the function fopen to open the file for reading. Here's the code to read-open the file we created in the PHP File Write lessons. PHP Code: $myFile = "testFile.txt"; $fh = fopen($myFile, 'r'); The file we created in the last lesson was named "testFile.txt".

TestFile.txt Contents: Floppy Jalopy Pointy Pinto Now that the file is open, with read permissions enabled, we can get started! The fread function is the staple for getting data out of a file. One character is equal to one byte. $myFile = "testFile.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, 5); fclose($fh); echo $theData; Display: Flopp The first five characters from the testFile.txt file are now stored inside $theData. Floppy Jalopy Pointy Pinto Floppy Jalopy. File Write. Now that you know how to open and close a file, lets get on to the most useful part of file manipulation, writing! There is really only one main function that is used to write and it's logically called fwrite.

Before we can write information to our test file we have to use the function fopen to open the file for writing. PHP Code: $myFile = "testFile.txt"; $fh = fopen($myFile, 'w'); We can use php to write to a text file. Below we are writing a couple of names into our test file testFile.txt and separating them with a carriaged return. $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Bobby Bopper\n"; fwrite($fh, $stringData); $stringData = "Tracy Tanner\n"; fwrite($fh, $stringData); fclose($fh); The $fh variable contains the file handle for testFile.txt. We wrote to the file testFile.txt twice. If you were to open the testFile.txt file in NOTEPAD it would look like this: Contents of the testFile.txt File: Bobby Bopper Tracy Tanner. Php $_REQUEST vs $_GET and $_POST.