I am not sure what is going wrong, nor from the official documentation I can get much help.
But this is what is happening.
I have option 'a', 'c:' and 'm:' to read from the command line using getOpt()
They are laid out as:
if ($op = getopt("a")) {
doAll();
} elseif ($op = getopt("c:")) {
doSome($op["c"]);
} elseif ($op = getopt("m:")) {
doOne($op["m"]);
} else {
echo "INVALID ARGS"; exit;
}
The option 'a' and 'm' works find but when I use option 'c' with:
php4 rebuild_mastertag.php -c 1234
It always prints INVALID ARGS
To make things more mysterious if I pass 1 or 12 or 12345678 it works but it doesn't for 123, 1234, 12345, 123456, 1234567 sort of numbers.
Really baffled.
Thank you for for your time.
Thanks #hchr
Your solution seems to work without trouble:
$op = getopt("ac:m:");
if (empty($op)) {
alertForInvalidArgument();
} else {
callProcessor($op);
}
function callProcessor($op)
{
if ($op["a"]) {
doAll();
} elseif ($op["c"]) {
doSome($op["c"]);
} else {
doOne($op["m"]);
}
}
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);
I need help.
As I do?
String same other string with different number.
Example
if ($current_server == "Lobby-01") {
echo "visiting in Lobby"
} elif ($current_server == "Lobby-02"){
echo "visiting in Lobby"
} and more..
I thought about trying
if ($current_server == "Lobby-/[0-99]+/"){
echo "visiting in Lobby"
} else {
//is false, then it will not show the message
}
is correct?
How do I do it?
Can you help me?
Use preg_match:
if (preg_match("/\bLobby-[0-9]{2}\b/i", $current_server)) {
echo "visiting in Lobby";
}
else {
// no message
}
Demo
my problem is, i have a form which i fill blabla and after i submit i need to check if the var '$number' contains only 9 numbers. which means that if it contains at least 1 letter or has less or more than 9 length it should return false, else it should return true;
this is what i got so far:
if (!is_numeric ($number) {
//do
} else {
}
1st problem: This code should take care of the only numbers part but it doesnt, it always returns false.
2nd: do you guys know of any way to take care of the 9 digits only verification?
thanks and sorry for my bad english, not my native language :P
Your number may contain unwanted whitespaces which cause the is_numeric() test not to work properly
So do the following: $number = trim($number); to remove them.
Then indeed this snippet is good to check if your variable is a number:
if (!is_numeric ($number)) {
//do
} else {
}
And for the number digits do a if statement to see if your number is between 100000000 and 999999999
So the full code will be:
$number = trim($number);
if (!is_numeric ($number)) {
//do
} else {
if ($number >= 100000000 && $number <= 999999999) {
// Everything is ok
} else {
}
}
Didn't understood your complete question coz of you native language :p, but i think you want this:
if (is_numeric($number) {
if(strlen($number) == 9){
return true;
} else {
return false;
}
} else {
echo 'Not a number';
}
Check if it contains digits and check whether its exactly contains 9.
$number = '123456789';
if(!preg_match('/^\d{9}$/', $number)) {
echo 'not ok';
} else {
echo 'ok';
}
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 => ""
);
I have an elseif statement, I'm confuse why is not working?
This my script
if(isset($namasupexp))
{
$supexp = $namasupexp; //condition 1
echo $supexp;
}
elseif(isset($namasupexp2))
{
$supexp = $namasupexp2; //condition 2 is not work, or ignore
echo $supexp;
}//end if
Why Only in condition1 is work, and other condition not work?
Can anyone tell me the solution or my misatake?
I really appreciate your answer. Thanks
if(isset($namasupexp))
{
// if this check is satisfied, it will stop checking any else/elseif statements after it
}
elseif(isset($namasupexp2))
{
}//end if
If you want both to be checked do,
if(isset($namasupexp))
{
}
if(isset($namasupexp2))
{
}
If the flow comes into condition 1, then it will never come into condition 2.
In your case, don't use elseif.
if(isset($namasupexp)) {
$supexp = $namasupexp; //condition 1
echo $supexp;
}
if(isset($namasupexp2)) {
$supexp = $namasupexp2; //condition 2
echo $supexp;
}