Proper syntax in PHP on form element
New to PHP-
If I have PHP getting a picture name from a form:
$pic=($_FILES['photo']['name']);
and I want to reference that field dynamically later in the file (sample_name.jpg):
$resizeObj = new resize('images/sample_name.jpg');
What is the proper formating?
Using $resizeObj = new resize('images/'$pic''开发者_StackOverflow);
gives me a syntax error.
Use
$resizeObj = new resize("images/$pic");
instead, and read this nice introduction to strings.
$resizeObj = new resize('images/'.$pic);
You may use
$resizeObj = new resize('images/'.$pic);
.
means string concatenation
There are several options:
$resizeObj = new resize("images/$pic");
is the most common, but I personally don't like variables in strings, so you could go with:
$resizeObj = new resize('images/'. $pic);
Or even
$resizeObj = new resize(sprintf('images/%s', $pic));
you can use 2 ways
$resizeObj = new resize("images/$pic");
or
$resizeObj = new resize('images/'.$pic);
精彩评论