I have the following code but really instead of matching the strings, I really want to do if $line, 88, 30 is not null then $status = "delivered";
if (preg_match("/pod/i", $filename)) {
if (substr($line, 88, 30) == 'SOMETHING') {
$status = "delivered";
} else {
continue;
}
}
else {
...
}
Am I right in thinking that I should wrap the substr() with an isset() or should I be using empty() ? I think I'm getting confused which way round it should go?
$line is below I only want to change status to delivered if a name exists
CUSTPOD1.003123797001868810003660228092015102800Mark
CUSTPOD1.003123797001867710003660128092015095700
I managed to get it to work with the following code, by storing the string in a var first I was then able to trim it and then use !empty (not empty) to check for characters
if (preg_match("/pod/i", $filename)) {
$somevar = trim(substr($line, 88, 2));
if (!empty($somevar)) {
$status = "delivered";
} else {
continue;
}
} else {
Related
The problem is with this line:
if $var LIKE '1800%';
and I'm not sure how to fix it. Thanks.
<?php
//check to see if account number is like 1800*
if (isset($_POST['acct_number'])) {
$var = $_POST['acct_number'];
if $var LIKE '1800%'; {
//stop the code
exit;
} else {
echo 'normal account number';
}
}
?>
You need PHP not MySQL. For 1800% just check that it is found at position 0:
if(strpos($var, '1800') === 0) {
//stop the code
exit;
} else {
echo 'normal account number';
}
If it can occur anywhere like %1800% then:
if(strpos($var, '1800') !== false) {
//stop the code
exit;
} else {
echo 'normal account number';
}
Use substr function to get first 4 characters and compare it with 1800.
if(substr($var, 0, 4) == '1800')
{
// your code goes here.
}
``
Another way could be to use strpos()
if (strpos($var, '1800') === 0) {
// var starts with '1800'
}
I would use a regular expression for this preg_match('/^1800.+/', $search, $matches);
Depending on whether 2 vars are set ($skip and $take) I want to do different things. I have a large if else statement, is there a more efficient way to write this?
if (isset($skip) && !isset($take)) {
//skip only
} elseif (!isset($skip) && isset($take)) {
//take only
} elseif (isset($skip) && isset($take)) {
//skip and take
} else {
//default
}
Edit
It should also be noted that this is to sit in a method where the vars will be set to null if not specified:
getAll($skip = null, $take = null)
You can simplify the logic a bit:
if (isset($skip) && isset($take)) {
// skip and take
} elseif (isset($skip)) {
// only skip
} elseif (isset($take)) {
// only take
} else {
// default
}
Since the OP clarified in a comment that this is inside a method, and both $skip and $take are arguments with default values, one might favor === over isset. Furthermore, you can re-arrange the logic a bit:
function getAll($skip = null, $take = null) {
if ($skip !== null && $take !== null) {
// both
} elseif ($skip !== null) {
// skip only
} elseif ($take !== null) {
// take only
} else {
// none
}
}
The === operator enforces an equality check with type safety.
The way default values for arguments work, the arguments are always guaranteed to be null if you don't pass them, so the equality check is a good way to check them here.
if you dont like if else
$switch = (int)isset($skip) + (int)isset($take)*2;
switch($switch){
case 0:
//default
break;
case 1:
//only skip
break;
case 2:
//only take
break;
case 3:
//skip and take
break;
}
I know there are like hundreds similar questions on this site, but I just can't get my code working...
I have this JSON
{
"version":"1.0.0",
"buildDate":20131029,
"buildTime":165127,
"lockPath":"..\\var\\lock",
"scriptPath":"..\\var\\lock",
"connections":
[
{
"name":"o016561",
"bez":"GEW-NRW",
"type":"OVPN"
},
{
"name":"o016482",
"bez":"GEW-BW",
"type":"OVPN"
},
{
"name":"o019998",
"bez":"GEW-SH",
"type":"OVPN"
}
]}
how can I access the "name" values to check if there's an existing file with an equal name?
I tried
$json_config_data = json_decode(file_get_contents($json_path,true));
foreach($json_config_data->connections as $connectionName)
{
if($connectionName->name == $fileName)
{
$status = 1;
}
else
{
$status = 0;
}
}
but I always get $status = 0...
I think there's an easy solution for this, but I'm pretty new to PHP so I'd be glad for any kind of help.
Thanks in advice
You're resetting the value of $status for every iteration which means that the last connection HAS to be the correct one. You're probably looking for a break statement.
$json_config_data = json_decode(file_get_contents($json_path,true));
$status = 0; //Default to 0
foreach($json_config_data->connections as $connectionName)
{
if($connectionName->name == $fileName)
{
$status = 1;
break; //End the loop
}
}
This would only result in $status == 1 if the final name matched your requirements; otherwise you're setting $status back to 0. You should break out of the loop when you find a match:
$status = 0;
foreach ($json_config_data->connections as $connectionName) {
if ($connectionName->name == $fileName) {
$status = 1;
break; // this breaks out of the foreach loop
}
}
I am retrieving some data from an in-house store and in case of failure, I get a very specific response. Calling strlen() on this variable returns the value of zero. It is also not equal to NULL or "". I'm using this code to test:
if ($data === NULL)
{
echo("data was null\n");
}
else if ($data === "")
{
echo("data was empty string\n");
}
else if (strlen($data) == 0)
{
echo("data was length zero\n");
}
This result is outputting data was length zero. What could the variable contain that is zero length, not null, and not the empty string?
Returned value must be false then.
echo strlen(false); // outputs 0
This may not being an answer. I can only answer if you present a var_dump($data); But I think also suprising for me is this:
$data = "\0";
if ($data === NULL)
{
echo("data was null\n");
}
else if ($data === "")
{
echo "data was empty string\n";
}
else if (strlen($data) == 0)
{
echo "data was length zero\n";
}
else
{
echo "something strange happened\n";
}
Output: something strange happened
:)
Try this :
$data = false;
I'm not sure why false has a strlen, but it does.
I'm currently writing up a function in order to validate a URL by exploding it into different parts and matching those parts with strings I've defined. This is the function I'm using so far:
function validTnet($tnet_url) {
$tnet_2 = "defined2";
$tnet_3 = "defined3";
$tnet_5 = "defined5";
$tnet_7 = "";
if($exp_url[2] == $tnet_2) {
#show true, proceed to next validation
if($exp_url[3] == $tnet_3) {
#true, and next
if($exp_url[5] == $tnet_5) {
#true, and last
if($exp_url[7] == $tnet_7) {
#true, valid
}
}
}
} else {
echo "failed on tnet_2";
}
}
For some reason I'm unable to think of the way to code (or search for the proper term) of how to break out of the if statements that are nested.
What I would like to do check each part of the URL, starting with $tnet_2, and if it fails one of the checks ($tnet_2, $tnet_3, $tnet_5 or $tnet_7), output that it fails, and break out of the if statement. Is there an easy way to accomplish this using some of the code I have already?
Combine all the if conditions
if(
$exp_url[2] == $tnet_2 &&
$exp_url[3] == $tnet_3 &&
$exp_url[5] == $tnet_5 &&
$exp_url[7] == $tnet_7
) {
//true, valid
} else {
echo "failed on tnet_2";
}
$is_valid = true;
foreach (array(2, 3, 5, 7) as $i) {
if ($exp_url[$i] !== ${'tnet_'.$i}) {
$is_valid = false;
break;
}
}
You could do $tnet[$i] if you define those values in an array:
$tnet = array(
2 => "defined2",
3 => "defined3",
5 => "defined5",
7 => ""
);