Skip to content

Instantly share code, notes, and snippets.

@ksnider
Created January 16, 2014 21:24
Show Gist options
  • Select an option

  • Save ksnider/8463693 to your computer and use it in GitHub Desktop.

Select an option

Save ksnider/8463693 to your computer and use it in GitHub Desktop.
<?php
// 80/20 PHP - Day Six - Strings
// http://api-demo.com/scripts/strings.php
// Using Single Quotes
echo 'This is a simple string<br/>';
echo 'You can also have embedded newlines in
strings this way as PHP ignores white space <br/>';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"<br/>';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n to a newline <br/>';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either<br/><br/><br/><br/>';
// Using Double Quotes
echo "This is a simple string<br/>";
echo "You can also have embedded newlines in
strings this way as PHP ignores white space <br/>";
// Outputs: Arnold once said: "I'll be back"
// echo "Arnold once said: "I\'ll be back"<br/><br/>";
// Variables Inside Quotes
// I have been doing this ...
$a = 1;
$b = 3;
echo 'a='. $a . ' and b= ' . $b . '<br/>';
// But with double quotes, you can do this
echo "a=$a and b=$b <br/>";
// or this ...
$juice = "apple";
$great = 'fantastic';
echo "He drank some $juice juice.<br/>"; // He drank some apple juice
echo "This is {$great}<br/>"; // This is fantastic
$juices = array("apple", "orange", "koolaid1" => "purple");
echo "He drank some $juices[1] juice.<br/>";
echo "He drank some $juices[koolaid1] juice.<br/>";
echo "He drank some {$juices[0]} juice.<br/><br/>";
// All because I am using double quotes
// String Operators
// Concatenate
echo $a = "Hello";
echo '<br/>';
echo $b = $a . "World!"; // now $b contains "Hello World!"
echo '<br/><br/>';
// Append
echo $a = "Hello"; echo '<br/>';
echo $a .= "World!"; // now $a contains "Hello World!"
// Explode
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo '<br/>';
echo $pieces[1]; // piece2
echo '<br/><br/>';
// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo '<br/>';
echo $pass; // *
// Using explode with the API
require("isdk.php");
$app = new iSDK;
if ($app->cfgCon("sandbox")) {
$returnFields = array('Groups');
$conDat = $app->loadCon(133, $returnFields);
echo "<pre>";
print_r($conDat);
echo "</pre>";
$tags = explode(",", $conDat['Groups']);
echo "<pre>";
print_r($tags);
echo "</pre>";
$count = 1;
foreach($tags as $tag) {
echo "loop Number $count <br/>"; // Notice the variable inside dbl quotes
if($tag == 128) {
echo "Found it! ";
break;
}
$count++;
}
} else {
echo "Not Connected...";
}
// Parse_URL
echo "<pre>";
print_r($_SERVER);
echo "</pre>";
$method = $_SERVER[REQUEST_METHOD];
$ip = $_SERVER[REMOTE_ADDR];
$qryStr = $_SERVER[QUERY_STRING];
echo "The request method was $method <br/>";
echo "My IP address is $ip <br/>";
echo "The query string was $qryStr <br/><br/>";
// Instead of using $_REQUEST, now we can use
if ($_SERVER[REQUEST_METHOD] == 'POST') {
echo 'This came from a campaign snippet';
} else {
echo 'Came from an action';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment