Skip to content

Instantly share code, notes, and snippets.

View tim-cotten's full-sized avatar

Tim Cotten tim-cotten

View GitHub Profile
@tim-cotten
tim-cotten / php_bugs_5d.php
Last active March 5, 2021 12:51
PHP Bugs: Arrays Don't Like Undefined Offsets (Example D)
<?php
// Set relative product URLs
foreach ($products as &$product) {
$url = "&product_id={$product['id']}";
$name = explode(" -- ", $product['name']); // like: Chocolates -- 587
$url .= "&name={$name[0]}";
if ($name[1] != "") {
$name[1] = str_pad($name[1], 8, "0", STR_PAD_LEFT); // format as 00000587
@tim-cotten
tim-cotten / php_bugs_5c.php
Created March 5, 2021 12:29
PHP Bugs: Arrays Don't Like Undefined Offsets (Example C)
<?php
$arr = array();
if ($arr[0] == false) {
echo "Yay";
} else {
echo "Boo";
}
@tim-cotten
tim-cotten / php_bugs_5b.php
Created March 5, 2021 12:24
PHP Bugs: Arrays Don't Like Undefined Offsets (Example B)
<?php
function startElement($name) {
global $n, $data_array, $message_type;
if (in_array($name, $message_type)) {
$data_array[$n] = "{$name}||";
} else {
$data_array[$n] = (!empty($data_array[$n]) ? $data_array[$n] : '') . "[{$name}||";
@tim-cotten
tim-cotten / php_bugs_5a.php
Created March 5, 2021 12:06
PHP Bugs: Arrays Don't Like Undefined Offsets (Example A)
<?php
function startElement($name) {
global $n, $data_array, $message_type;
if (in_array($name, $message_type)) {
$data_array[$n] = $name . "||";
} else {
$data_array[$n] .= "[".$name."||";
@tim-cotten
tim-cotten / php_bugs_4g.php
Last active March 5, 2021 02:22
PHP Bugs: Confusing Non-Objects for Objects (Example G)
<?php
function exchange_send_message_raw($context, $content)
{
$location = "https://test.cotten.io/exchange/raw_message.php";
$request = array('direction' => 1, // outgoing
'context' => $context,
'content' => json_encode($content));
$message = null;
@tim-cotten
tim-cotten / php_bugs_4f.php
Last active March 5, 2021 02:10
PHP Bugs: Confusing Non-Objects for Objects (Example F)
<?php
function exchange_send_message_raw($context, $content)
{
$location = "https://test.cotten.io/exchange/raw_message.php";
$request = array('direction' => 1, // outgoing
'context' => $context,
'content' => json_encode($content));
$response = ProxyPostClient::request($location, $request);
$response_obj = json_decode($response);
@tim-cotten
tim-cotten / php_bugs_4e.php
Created March 4, 2021 21:42
PHP Bugs: Confusing Non-Objects for Objects (Example E)
<?php
// Exclude orders that are drop-ship products from store lookups
if ($order->product->type == PRODUCT::TYPE_DROPSHIP) {
return false;
}
@tim-cotten
tim-cotten / php_bugs_4d_order.php
Created March 4, 2021 21:33
PHP Bugs: Confusing Non-Objects for Objects (Example D: Order Structure)
// Order structure example
Order Object
(
  [id] => 44274002
  [delivery_date] => 2021–03–05
  [delivery_location_type] => Home
  [occasion] => Just Because
  [fulfillment_type] => 3
  [fulfillment_location] => 12
[product] => Product Object
@tim-cotten
tim-cotten / php_bugs_4d.php
Created March 4, 2021 21:19
PHP Bugs: Confusing Non-Objects for Objects (Example D)
<?php
// Exclude orders with drop-ship items from store lookups
foreach ($order->product as $_product) {
if ($_product->type == PRODUCT::TYPE_DROPSHIP) {
return false;
}
}
@tim-cotten
tim-cotten / php_bugs_4c.php
Created March 4, 2021 21:10
PHP Bugs: Confusing Non-Objects for Objects (Example C)
<?php
// ... Missile Command API called ...
$api_result = "{launch_verification:false,msg:'abort'}";
$result = json_decode($api_result);
if (isset($result->launch_verification) && $result->launch_verification == true) {
echo "Launching nuclear missiles.";
launch_missiles();
exit();