Skip to content

Instantly share code, notes, and snippets.

View jrtashjian's full-sized avatar
🪓
Axe-ceptional at axing code

JR Tashjian jrtashjian

🪓
Axe-ceptional at axing code
View GitHub Profile
<?php
/**
* The only function of this statement is to return $poll_id if it's set. Which
* snippet would you use and why?
*
* I (@jrtashjian) prefer and wrote snippet 1. I like how contained the
* function is, and the fact I'm only calling the post() function once.
*
* Each snippet works correctly, this is just preference. I find it redundant
* to call the post() function more than once (as shown in snippet 3). I also
<?php
function fetch( $url )
{
print "Crawling : $url\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
<?php
while( $i < 100 )
{
$i++;
if( $i % 15 == 0 ) { echo "FizzBuzz\n"; continue; }
if( $i % 3 == 0 ) { echo "Fizz\n"; continue; }
if( $i % 5 == 0 ) { echo "Buzz\n"; continue; }
echo "$i\n";
}
@jrtashjian
jrtashjian / gist:1051145
Created June 28, 2011 13:35
Passing params as an array
<?php
function create( $params = array() )
{
$defaults = array(
'param_one' => '',
'param_two' => time(),
'param_three' => 0,
);
$params = array_merge($defaults, $params);
@jrtashjian
jrtashjian / gist:1092582
Created July 19, 2011 14:41
Chunk File
<?php
/**
* This function will read the file passed to it at 1MB/s and output it to the
* browser.
*/
function readfile_chunked( $filename )
{
$rate = 1024;
<?php
// not safe
exec($_GET['query']);
exec( addslashes($_GET['query']) );
// THE SAFE ROUTE
exec( escapeshellarg($_GET['query']) );
@jrtashjian
jrtashjian / gist:1218093
Created September 14, 2011 23:22
short-circuiting loops
<?php
/**
* Improper:
* - can become hard to read if the else block is large
* - anytime you indent you decrease legibility.
* - only applicable for this instance where an item will be skipped and you continue
* or break a loop.
*/
foreach( $fields as $field )
@jrtashjian
jrtashjian / gist:1277914
Created October 11, 2011 12:08
Infinite Copy Loop
// Not really an infinite loop as the system exits the process eventually, caused by the name being too long.
// This is what I did:
macbook:~ $ mkdir /var/www/directory/
macbook:~ $ cp -R [bunch of files/directories] /var/www/directory/
macbook:~ $ cd /var/www/directory
// at this point I forgot to create a subdirectory I wanted these files to be in, inside the directory I just created.
macbook:directory/ $ mkdir /var/www/directory/example/
@jrtashjian
jrtashjian / gist:1558027
Created January 4, 2012 01:51
Raptor Stokes
Create a bookmark with the following as the address:
javascript:document.getElementById('noah-stokes').getElementsByTagName('header')[0].getElementsByTagName('img')[0].setAttribute('src', 'http://f.cl.ly/items/1p2a2n191f1y3U1a2J3w/raptorstokes.jpg');
Then go to this page: http://thegreatdiscontent.com/noah-stokes and click the bookmark
RAPTOR STOKES!
<?php
// unnecessary
if( strlen($_POST['zip']) < 6 && strlen($_POST['zip']) > 4 )
// better
if( strlen($_POST['zip']) == 5 )