Sponsored Content
Top Forums Shell Programming and Scripting PHP: declared variables, strlen vs isset Post 302976550 by stomp on Thursday 30th of June 2016 05:51:02 PM
Old 06-30-2016
Hi crimso,

Quote:
...and to limit how big the post got.
This is very appreciated indeed.

First of all. There are functions and libs for everything. Command-Line-Option Parsing may be done with getopt: Look here: PHP: getopt - Manual

(But you may ignore that, if your code works).

The following does not work:

Code:
<?php 
#!/usr/bin/php -q

If you decide to use #!, it has to be at the very beginning of the Script-File. Only then the kernel will read the following binary as interpreter for the actual file.

Further without testing the code myself, I suggest you test, if your Arg-parsing produces correct output. I suspect it may not fully work as expected and the values are not what you assume them to be. I suggest you insert some debug statements before+after Arg parsing like:

Code:
print_r($parms);
print_r($arg);
print_r($value);
print_r($host);
print(gettype($host));

A logging function would help you to debug your code independently of the running environment(webserver or command line).

Code:
#!/usr/bin/php
<?PHP
# global log-file handle
$log = NULL;
function mylog($msg=NULL) {
       $logfile="/tmp/myapp.log";  # There may be better locations than /tmp for the logfile
       global $log;
       if(!$log) {   
            # only open logfile once per script run. After the first
            # mylog() invocation the open filehandle will be used.
            # Also be lazy and let it automatically be closed at 
            # end of programm by php itself
          $log = fopen($logfile,"a");
          if(! $log) die("Cannot open Logfile: " . error_get_last());
       }
       if($msg) {
                fwrite($log,date("Y/m/d h:i:s",mktime())." : ".getmypid()." : ".(string)$msg."\n");
       }
}

The above mentioned debug statements are easily usable with such a log function(print_r needs to be told, that it returns the formated dump data and so it does not print it to stdout itself).

Code:
mylog(print_r($host,true));

PHP: Hypertext Preprocessor

PHP.net is a great and fabulous source of documentation for PHP. Nearly all functions documentation pages have directly attached real-life coding examples down at the bottom of each page.
 

10 More Discussions You Might Find Interesting

1. Programming

Problems with Strlen

hello, i have a problem with strlen. I have written this: for(y=13,z=0; cInBuf!=' ';y++) { cBuf=cInBuf; z++; } len = strlen(cBuf); out=len/2; fprintf(outfile,"F%i",out); If strlen is e.g. 22, it write F22. I want to write F2F2. How can i do this?... (5 Replies)
Discussion started by: ACeD
5 Replies

2. Shell Programming and Scripting

Problem with the strlen function in ksh

Hello, Just a little problem with the ksh function : strlen I want to use this function in this little ksh program : while read line ; do TOTO=$line TOTONB=strlen($TOTO) echo $TOTONB (3 Replies)
Discussion started by: steiner
3 Replies

3. Shell Programming and Scripting

accessing variables declared in another perl script

Hi all, I have a perl script which declares two variables and calls another perl script which accesses those variables. But I am unable to access the variables in the called script. My script is as follows: my $ENV{a}="20"; system("perl called.pl"); and my called.pl contains: print... (3 Replies)
Discussion started by: gurukottur
3 Replies

4. Programming

'strlen' of a constant string

In a declaration, I have: const char comment_begin = "<!--"; const char comment_end = "-->"; const int comment_begin_len = strlen(comment_begin); const int comment_end_len = strlen(comment_end); When I compile, I get the warnings: emhttpc.c:64: warning: initializer element is not... (10 Replies)
Discussion started by: cleopard
10 Replies

5. Programming

pointer arithmetic vs. strlen() & strnlen()?

I have been getting some flack recently for my use of strlen() and strnlen(). Honestly I have always just taken their functionality for granted as being the easiest way of getting the length of a string. Is it really so much better to do pointer arithmetic? What am I gaining besides more... (3 Replies)
Discussion started by: jjinno
3 Replies

6. Shell Programming and Scripting

isset() PHP function in cgi bash scripts

Hi! Some minutes ago I've posted a question related with sed regexps because I need to catch information sended with forms with GET action. This is the post: https://www.unix.com/shell-programming-scripting/127800-regular-expression-sed.html But now I have a new question. Does cgi scripts have... (0 Replies)
Discussion started by: GagleKas
0 Replies

7. Programming

strlen for UTF-8

My OS (Debian) and gcc use the UTF-8 locale. This code says that the char size is 1 byte but the size of 'a' is really 4 bytes. int main(void) { setlocale(LC_ALL, "en_US.UTF-8"); printf("Char size: %i\nSize of char 'a': %i\nSize of Euro sign '€': %i\nLength of Euro sign: %i\n",... (8 Replies)
Discussion started by: cyler
8 Replies

8. AIX

`pthread_rwlock_t' was not declared in this scope

Hello All, I am getting this error while compiling my application on IBM AIX 5.3. As I tried to define _XOPEN_SOURCE=500 in makefile, that didn't work. Please help us to resolve the error. (0 Replies)
Discussion started by: mustus
0 Replies

9. Shell Programming and Scripting

Help in separating variables declared in the main function

Hi! I've a C program as shown below.. The line numbers and the statements of the program are separated by a space.. 1 #include<stdio.h> 2 char a,b,c; 3 float x,y,z; 4 int main() 5 { 6 int d,e,f; 7 // further declarations 8 // further declarations 9 /* body*/ 10 } 11 void fun1() 12... (1 Reply)
Discussion started by: abk07
1 Replies

10. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies
ISSET(3)								 1								  ISSET(3)

isset - Determine if a variable is set and is not NULL

SYNOPSIS
bool isset (mixed $var, [mixed $...]) DESCRIPTION
Determine if a variable is set and is not NULL. If a variable has been unset with unset(3), it will no longer be set. isset(3) will return FALSE if testing a variable that has been set to NULL. Also note that a null character ( "") is not equivalent to the PHP NULL constant. If multiple parameters are supplied then isset(3) will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered. PARAMETERS
o $var - The variable to be checked. o $... - Another variable ... RETURN VALUES
Returns TRUE if $var exists and has value other than NULL, FALSE otherwise. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.4.0 | | | | | | | Checking non-numeric offsets of strings now | | | returns FALSE. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 isset(3) Examples <?php $var = ''; // This will evaluate to TRUE so the text will be printed. if (isset($var)) { echo "This var is set so I will print."; } // In the next examples we'll use var_dump to output // the return value of isset(). $a = "test"; $b = "anothertest"; var_dump(isset($a)); // TRUE var_dump(isset($a, $b)); // TRUE unset ($a); var_dump(isset($a)); // FALSE var_dump(isset($a, $b)); // FALSE $foo = NULL; var_dump(isset($foo)); // FALSE ?> This also work for elements in arrays: <?php $a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple')); var_dump(isset($a['test'])); // TRUE var_dump(isset($a['foo'])); // FALSE var_dump(isset($a['hello'])); // FALSE // The key 'hello' equals NULL so is considered unset // If you want to check for NULL key values then try: var_dump(array_key_exists('hello', $a)); // TRUE // Checking deeper array values var_dump(isset($a['pie']['a'])); // TRUE var_dump(isset($a['pie']['b'])); // FALSE var_dump(isset($a['cake']['a']['b'])); // FALSE ?> Example #2 isset(3) on String Offsets PHP 5.4 changes how isset(3) behaves when passed string offsets. <?php $expected_array_got_string = 'somestring'; var_dump(isset($expected_array_got_string['some_key'])); var_dump(isset($expected_array_got_string[0])); var_dump(isset($expected_array_got_string['0'])); var_dump(isset($expected_array_got_string[0.5])); var_dump(isset($expected_array_got_string['0.5'])); var_dump(isset($expected_array_got_string['0 Mostel'])); ?> Output of the above example in PHP 5.3: bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) Output of the above example in PHP 5.4: bool(false) bool(true) bool(true) bool(true) bool(false) bool(false) NOTES
Warning isset(3) only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined(3) function. Note Because this is a language construct and not a function, it cannot be called using variable functions. Note When using isset(3) on inaccessible object properties, the __isset() overloading method will be called, if declared. SEE ALSO
empty(3), __isset(), unset(3), defined(3), the type comparison tables, array_key_exists(3), is_null(3), the error control @ operator. PHP Documentation Group ISSET(3)
All times are GMT -4. The time now is 04:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy