Archive for the ‘php’ Category

Simple php based mysql query script

<?php   $dbname = ‘dbname’; $dbuser = ‘user’; $dbpass = ‘pass’; $dbhost = ‘hostname’;   $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to ‘$dbhost’"); mysql_select_db($dbname) or die("Could not open the db ‘$dbname’"); $test_query = "SELECT 1,2,3,4 FROM yourtable"; $result = mysql_query($test_query); $tblCnt = 0; while($tbl = mysql_fetch_array($result)) { $tblCnt++; echo $tbl[0]."<br />\n"; echo […]

Php Nearest Match

function getNearestMatch($number, $candidates) {   $candidates = is_array($candidates) ? $candidates : array($candidates);   $size = count($candidates); if ($size > 0) { //absolute difference between 0th element and input value $diff = abs($candidates[0] – $number); //element closest to input value $ret = $candidates[0]; //loop through the rest of the array for ($i = 1; $i < […]

Yahoo finance rates convert (for educational purposes only)

<?php class Rates { function Convert($amount,$from,$to){ $url="http://adsynth-ofx-quotewidget-prod.herokuapp.com/api/1"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, ‘{"method":"spotRateHistory","data":{"base":"’.$from.’","term":"’.$to.’","period":"week"}}’); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array(’Content-Type:application/json’)); curl_setopt($ch, CURLOPT_REFERER, "https://widget-yahoo.ofx.com/"); $html = curl_exec ($ch); curl_close ($ch); $veri=json_decode($html,true); return $veri["data"]["CurrentInterbankRate"]*$amount; } } $kur = new Rates(); echo $kur->Convert(1, ‘USD’, ‘GBP’);

PHP arrays comparision to Javascript and Java

Associative arrays in PHP = Javascript Objects written as name value pairs = Hash maps in Java

Regex keep only alphanumeric characters with unicode support

echo preg_replace("/[\W]/u", "", $str);

Php check if English date time format correct

if(preg_match(’/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/’,$datetime)) { echo "correct"; }

Does PHP have a virtual machine like Java?

Yes. Independently from the platform PHP is running on, the scripts are compiled into the same bytecode and run by the Zend Engine. The difference from Java is that this compiled code is usually not stored into separate files and the scripts are re-compiled on each execution (however, see opcode caches). Another important difference between […]

Difference between abstract class and interface

In short, in case of full abstraction of methods you want to define use interface, in case of partial abstraction, if some methods you want to define are common among concrete classes but some are not then use abstract class Abstract class can have definitions of constants, abstract methods or method stubs (methods without body/implementation) […]

Top 10 security vulnerabilities by OWASP

A1 Injection (PHP include, global variables, Sql Injection through Url) A2 Broken authentication (To avoid broken authentication put into practice not leaving the login page for admins publicly accessible. Rename /wp-admin/ on WordPress to something else. Rename /admin in Magento to something else) A3 Sensitive data exposure (arp spoofing resulting traffic sniffing, stealing auth passwords, […]

4 Main principles of modern object based programming

Abstraction (Abstract classes and Interfaces) > Inheritance (class extends parent Abstract or concrete class). Encapsulation (blackbox, isolation methods, getters, setters, Creational Patterns) Polymorphism (overriding, logical cluster principle – many classes (nodes) on same level with diff implementation of same function (operation), Operational patterns, Strategy pattern) Inheritance – пожалуй, важнейшая особенность object based programming. Если требуется […]