As you know in JavaScript when you define a variable with the special word “var” the scope of this variable is within the function. So when you simply wite “var a = 5” the variable named “a” has a global scope and can be accessed in any function in the global scope.
var a =5;function f(){return a;}// returns 5
var a = 5;
function f() { return a; } // returns 5
Thus f will return the value of “a” which equals to 5. You can also change the value of the global variable in the function body.
var a =5;function f(){ a =10;return a;}
console.log(a);// equals to 10
var a = 5;
function f() { a = 10; return a; }
console.log(a); // equals to 10
Now after we call the function f the value of “a” will equal to 10. This is because we reference the global variable “a” into the function body without using the keyword “var”. This means that if you put the “var” keyword the variable “a” inside the function body is no longer the same variable as the variable defined outside the body. It becames “local” and it’s visible only inside the function. Continue reading OOP JavaScript: Accessing Public Methods in Private Methods→
You should know that in JavaScript you cannot simply write a multilined code chunk, i.e. something like this:
var str ='hello
world';
var str = 'hello
world';
This will result in an error, which can be a problem when you deal with large strings. Imagine a string containing some HTML that should be injected via innerHTML. Now one possible solution is to concatenate two or more strings, by splitting the initial string.
var str ='hello'+'world';
var str = 'hello'
+ 'world';
Howerver this solution result in some additional operations like concatenation. It can be very nice if there was something like the PHP multiline string format.
The PHP Example
In PHP you have the same use case with strings. You can have a string concatenated over multiple lines, but you cannot split lines like so:
// the following line is wrong$str='hello
world';// a possible solution to the line above is$str='hello'.'world';// which is very similar to the js solution in the second example// ... but in PHP there is the so called HEREDOC$str=<<<EOD
hello
world
EOD;
// the following line is wrong
$str = 'hello
world';
// a possible solution to the line above is
$str = 'hello'
. 'world';
// which is very similar to the js solution in the second example
// ... but in PHP there is the so called HEREDOC
$str = <<<EOD
hello
world
EOD;
The heredoc gives us the power to write multiline strings.
The JavaScript Trick
Actually in JavaScript there is something that’s exactly what Heredoc is meant to be for PHP. Take a look at the last example:
var text =<>this<br />
is
my
multi-line
text
</>.toString();
document.body.innerHTML= text;
var text = <>
this <br />
is
my
multi-line
text
</>.toString();
document.body.innerHTML = text;
Thus you can write multiline strings in the JavaScript code.
It’s a well know fact that you can preform HTTP requests with CURL. Zend Framework does the same job with Zend_Http. Especially Zend_Http_Client can be used to “replace” the usual client – the browser, and to perform some basic requests.
I’ve seen mostly GET requests, although Zend_Http_Client can perform various requests such as POST as well.
// new HTTP request to some HTTP address$httpClient=new Zend_Http_Client('http://www.example.com/');// GET the response$response=$httpClient->request(Zend_Http_Client::GET);
// new HTTP request to some HTTP address
$httpClient = new Zend_Http_Client('http://www.example.com/');
// GET the response
$response = $httpClient->request(Zend_Http_Client::GET);
Here’s a little snippet showing how to POST some data to a server.
// new HTTP request to some HTTP address$client=new Zend_Http_Client('http://www.example.com/');// set some parameters$client->setParameterPost('name','value');// POST request$response=$client->request(Zend_Http_Client::POST);
// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);
Note that the request method returns a response. Thus if you are simulating a form submit action you can “redirect” to the desired page just like the form.
// new HTTP request to some HTTP address$client=new Zend_Http_Client('http://www.example.com/');// set some parameters$client->setParameterPost('name','value');// POST request$response=$client->request(Zend_Http_Client::POST);echo$response->location;
// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);
echo $response->location;
Here’s the tricky part. As we know from the exec() man page this function executes the given command. As described there, the exact declaration of this method is:
string exec( string $command[,array&$output[, int &$return_var]])
However sometimes that is not so true. Let’s see an example with the popular FFMPEG program. In one hand when you try to start a video converting program with PHP you can simply call exec with the FFMPEG command as the first parameter. The output is OK and you can dump it as in the example here.
In some cases you can call FFMPEG only with the -i option to get some info about the input file. Such info can be the size of the image, the bitrate of the source file or the length of the movie. Executing this command:
in the terminal you’ll see the output you wish. However if you trying to execute the command via exec() the output remains empty?
Why the Output Parameter is Empty?
Looking at the terminal screen you can’t see the difference, but actually in the second case, when you execute FFMPEG only with the -i option, the output is redirected to the standard error output. That’s why you can’t see it in the exec() second parameter. However there is a solution.
Solution and Conclusion
The solution is quite simple. You’ve to redirect the output of the FFMPEG command to the standard output.
This showcase with the FFMPEG is important not only because now you can call FFMPEG’s command without problems, but because you now know that exec() can’t read the standard error output. Thus you’ve to be careful and always redirect to the standard output.
I’ve posted about Zend_Http_Client. Simply there you can ‘make’ your own http client and you can request a remote file. Just to check what’s going on with this file.
// new HTTP request to a file$httpClient=new Zend_Http_Client('http://www.example.com/myfile.mp4');// get the HEAD of the response and match agains the// Content-Length. That's because using the Content-Type is slower$response=$httpClient->request(Zend_Http_Client::HEAD);// if the Content-Length is 0 the file doesn't existsif(0===(int)$response->getHeader('Content-Length')){echo'the file doesn\'t exits';}
// new HTTP request to a file
$httpClient = new Zend_Http_Client('http://www.example.com/myfile.mp4');
// get the HEAD of the response and match agains the
// Content-Length. That's because using the Content-Type is slower
$response = $httpClient->request(Zend_Http_Client::HEAD);
// if the Content-Length is 0 the file doesn't exists
if (0 === (int)$response->getHeader('Content-Length')) {
echo 'the file doesn\'t exits';
}
However is there any other way to answer the same question?
fopen()
Yes and no? Perhaps yes, but you should be careful. I’m still not sure it can be used in any case. However here’s the snippet.